2

My code for a horizontal bar chart with a summarizing boxplot is coming together, but I still have a nagging PGF Math problem if I try to plot the boxplot values, which are commented out at the bottom of my code:

! Package PGF Math Error: Could not parse input 'auto' as a floating point numb
er, sorry. The unreadable part was near 'auto'..

See the PGF Math package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.139 \end{tikzpicture}  }

My MikTeX has PGFPLOTS 1.10 and is using it. I have been going through the manuals for PGFPLOTS and PGFPLOTSTABLE, and the forum here looking for and finding much useful information. My code is as follows:

\documentclass[margin=1cm]{standalone}
\usepackage[usenames,dvipsnames]{color}
\usepackage{amsmath,relsize} 

\usepackage{filecontents} 
\usepackage{pgfplots,pgfplotstable}
\usepgfplotslibrary{statistics}

\pgfplotsset{every axis/.append style={
             semithick,tick style={
             major tick length=4pt,thick,black}},
             compat=1.10, 
%
             discard if not/.style 2 args={
              x filter/.code={
              \edef\tempa{\thisrow{#1}}
              \edef\tempb{#2}
              \ifx\tempa\tempb
             \else
                \def\pgfmathresult{inf}
             \fi
             }
            },
}

\pgfkeys{/pgfplots/x axis shift up/.style={
    x axis line style={yshift=#1},
    xtick style={yshift=#1},
    xticklabel shift={#1}}}
\pgfkeys{/pgfplots/y axis shift left/.style={
    y axis line style={xshift=-#1},
    ytick style={xshift=-#1},
    yticklabel shift={#1}}}

\begin{document}
\sffamily
\begin{filecontents}{data.csv}
Miles,   Ride, Bike,    rdate 
12.5,    1,    Yel-11,  {$\stackrel{\hbox{Yel-11}}{\hbox{\tiny 03/30/14}}$}
16.5,    2,    Fuji,    {$\stackrel{\hbox{Fuji}}{\hbox{\tiny 04/23/14}}$}
21.0,    3,    8000SHX, {$\stackrel{\hbox{8000SHX}}{\hbox{\tiny 05/18/14}}$}
24.2,    4,    Yel-11,  {$\stackrel{\hbox{Yel-11}}{\hbox{\tiny 05/24/14}}$}
23.4,    5,    YFoil,   {$\stackrel{\hbox{YFoil 77}}{\hbox{\tiny 05/29/14}}$}
21.1,    6,    Y-22,    {$\stackrel{\hbox{Y-22}}{\hbox{\tiny 05/30/14}}$}
23.9,    7,    YFoil,   {$\stackrel{\hbox{YFoil 77}}{\hbox{\tiny 06/01/14}}$}
32.5,    8,    Fuji,    {$\stackrel{\hbox{Fuji}}{\hbox{\tiny 06/05/14}}$}
\end{filecontents}

\pgfplotstablegetrowsof{data.csv}
\edef\numberofrows{\pgfplotsretval}
\pgfmathsetmacro\numberofticks{\numberofrows-1}

\fbox{ 
\begin{tikzpicture}
\begin{axis}[clip=false, domain=0:50, unbounded coords=jump,
     width=0.90\linewidth,height=8.5cm,
     xbar,/pgf/bar shift=0pt,
     title={\centering Bike Miles for 2014},
     title style={yshift=1cm, text width=0.90\linewidth,
     font=\rmfamily\itshape\Large},
     xmin=0, xmax=50, xticklabel pos=upper, 
     tick align=outside,
     xticklabel style={major tick length=20pt, 
                       color=black, tickwidth={10pt}}, 
     axis x line*=top, axis y line*=left, 
     y axis line style={opacity=0},
     xmajorgrids, major grid style=black, axis on top,
     xtick={0,10,15,20,25,30,35,40,50},
     separate axis lines, x axis shift up=15pt, 
     y axis shift left=10pt, enlarge y limits=0.0,
     ytick={0,...,\numberofticks}, y dir=reverse,
     y tick label style={align=right, major tick length=5pt},
     yticklabels from table={data.csv}{[index]3},
     nodes near coords, nodes near coords align=horizontal,
     ]

\addplot [draw,fill=blue!75!black, discard if not={Bike}{Fuji}] 
        table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};

\addplot [draw,fill=blue, discard if not={Bike}{YFoil}] 
        table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};

\addplot [draw,fill=yellow, discard if not={Bike}{Yel-11}] 
        table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};

\addplot [draw,fill=gray, discard if not={Bike}{8000SHX}] 
        table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};

\addplot [draw,fill=red, discard if not={Bike}{Y-22}] 
        table [x=Miles, y expr=\coordindex, col sep=comma]{data.csv};

\draw [red,dotted,thick] (axis cs:21.9,-0.5) -- (axis cs:21.9,\numberofrows);

\draw [black] (axis cs:12.5,-0.5) -- (axis cs:32.5,-0.5);
\draw [red,thick=4mm] (axis cs:17.9,-0.5) -- (axis cs:25.9,-0.5)
      node[anchor=north east] at (axis cs:21.9,-0.5){\small $ \mu$}
      node[anchor=north east] at (axis cs:17.9,-0.5){\small $-\sigma$}
      node[anchor=north west] at (axis cs:25.9,-0.5){\small $+\sigma$};

\addplot+[
  boxplot prepared={ draw position=\numberofrows,
    lower whisker =12.50,
    lower quartile=19.88,
            median=22.25,
    upper quartile=23.98,
    upper whisker =32.50,
        box extend=0.75,  % height of box
    whisker extend=0.50,  % height of whiskers
    every box/.style={thick,dashed,draw=blue,fill=gray!50},
    every whisker/.style={black,thick},
    every median/.style={densely dotted,cyan,thick},
  },
] coordinates{};
%%[above]
%%node[left] at
%%(boxplot box cs: \boxplotvalue{lower whisker},1)
%%{\pgfmathprintnumber{\boxplotvalue{lower whisker}}}
%%node[left] at
%%(boxplot box cs: \boxplotvalue{lower quartile},1)
%%{\pgfmathprintnumber{\boxplotvalue{lower quartile}}}
%%node[left] at
%%(boxplot box cs: \boxplotvalue{median},0.5)
%%{\pgfmathprintnumber{\boxplotvalue{median}}}
%%node[right] at
%%(boxplot box cs: \boxplotvalue{average},0.5)
%%{\pgfmathprintnumber{\boxplotvalue{average}}}
%%node at
%%(boxplot box cs: \boxplotvalue{upper quartile},1)
%%{\pgfmathprintnumber{\boxplotvalue{upper quartile}}}
%%node[right] at
%%(boxplot box cs: \boxplotvalue{upper whisker},0.5)
%%{\pgfmathprintnumber{\boxplotvalue{upper whisker}}};
\draw  node[anchor=center] at (axis cs:40.0,9) {\numberofrows\ rides for 175.1 miles as of 06/05/14};

\end{axis}

\end{tikzpicture}  }%fbox
\end{document}

which generates: enter image description here

I tried putting the data into a macro from the manual and examples here, but that only exasperates the situation and doesn't work. There must be a fundamental problem with the way the table is read. I added the domain=0:50, unbounded coords=jump, to the axis options and there were no more complaints about out of bound points not plotted for every data line, for every addplot call.

NOTE: coordinate (12.5,0Y0.0e0]) has been dropped because it is unbounded (in x
). (see also unbounded coords=jump). 

Seems like the x=Miles, y expr=\coordindex is having problems, though it still plots the bar. In either case, trying to print/label the boxplot values for (lw, q1, q2,q3, uw) crashes the compile (above error message).

Also, how do I shift the major grid lines up with the x axis and stop them from extending past the bottom bar? They are normally white so they're only visible through the bars.

I'm trying to learn PGFPLOTS using this simple/sample data set. I have the two PGF manuals on my iPad in iBooks, but the form of the pdf's causes iBooks to crash often. The LaTeX Writer app for the iPad runs PGFPLOTS rather well, though PGFPLOTS has to be updated manually to version 1.10.

I would greatly appreciate any help here. Thanks in advance. Regards, Dave

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.