6

I have a version of this working with LaTeX Writer and PGFPLOTS 1.5.1 on my iPad. Moving over to Linux and TeXLive 2013 with PGFPLOTS 1.10 sees the Y AXIS LABELS disappear after the first. The bars seem correct otherwise. I'm trying to reproduce some XBAR plots created with PicTeX, but each bar colored according to a category label in the data file. Here is what I have now:

XBAR Chart with categories color coded.

\documentclass{standalone}
\usepackage[margin=1in]{geometry}
\usepackage{filecontents} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\pgfplotsset{width=.90\textwidth, compat=newest} 

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

\begin{document}

\begin{filecontents}{data.csv}
Miles,   Bike,    rdate
215,     Yel-11,  {Yel-11    03/24/14}
159,     Fuji,    {Fuji      05/23/14}
134,     8000SHX, {8000SHX   03/25/14}
106,     Fuji,    {Fuji      05/25/14}
58,      Y-22,    {Y-22      05/12/14}
55,      Yfoil,   {Y Foil 77 05/25/14}
\end{filecontents}

\pgfplotstablegetrowsof{data.csv}

\begin{tikzpicture}
\begin{axis}[
%/pgf/number format/.cd,1000 sep={},
width=0.9\linewidth,height=6cm,
xbar,/pgf/bar shift=0pt,
xmin=0, xmax=250, xticklabel pos=upper, tick align=outside,
xticklabel style={major tick length=20pt, color=black,tickwidth={10pt}},
axis x line*=top, %tickwidth={2pt},
axis y line*=left, xtick={0,50,100,150,200,250},
%xtick=\empty, %xlabel= {},
%enlarge x limits={value=0.1, upper},
enlarge y limits=0.1,
ytick={0,...,\pgfplotsretval}, 
y dir=reverse,
y tick label style={major tick length=5pt},
yticklabels from table={data.csv}{[index]2},
nodes near coords, nodes near coords align=horizontal,
%point meta=explicit symbolic
]

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

\addplot [draw,fill=blue!60!white, discard if not={Bike}{Yfoil}] 
        table [y expr=\coordindex, x index=0, col sep=comma]{data.csv};

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

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

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

\end{axis}
\end{tikzpicture}

\end{document}

I am hoping to do number of these plots like this with 40-50 entries per plot. How do I get the Y AXIS labels to display again. Is there an easier way to do this? How can I apply Tufte style features such as shifted axis on top, no axis on left, a multi-line title on top, etc.? I'm still new to PGFPLOTS, and any and all help is greatly appreciated. Regards, Dave

0

1 Answer 1

6

At the time when you use \pgfplotsretval, it doesn't contain the number of rows anymore. To fix the problem, save the result in a different macro, using something like \edef\numberofrows{\pgfplotsretval} and then use that in the ytick key:

\documentclass[border=5mm]{standalone}
\usepackage{filecontents} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\pgfplotsset{width=.90\textwidth, compat=newest} 

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

\begin{document}

\begin{filecontents}{data.csv}
Miles,   Bike,    rdate
215,     Yel-11,  {Yel-11    03/24/14}
159,     Fuji,    {Fuji      05/23/14}
134,     8000SHX, {8000SHX   03/25/14}
106,     Fuji,    {Fuji      05/25/14}
58,      Y-22,    {Y-22      05/12/14}
55,      Yfoil,   {Y Foil 77 05/25/14}
\end{filecontents}

\pgfplotstablegetrowsof{data.csv}
\edef\numberofrows{\pgfplotsretval}

\begin{tikzpicture}
\begin{axis}[
%/pgf/number format/.cd,1000 sep={},
width=0.9\linewidth,height=6cm,
xbar,/pgf/bar shift=0pt,
xmin=0, xmax=250, xticklabel pos=upper, tick align=outside,
xticklabel style={major tick length=20pt, color=black,tickwidth={10pt}},
axis x line*=top, %tickwidth={2pt},
axis y line*=left, xtick={0,50,100,150,200,250},
%xtick=\empty, %xlabel= {},
%enlarge x limits={value=0.1, upper},
enlarge y limits=0.1,
ytick={0,...,\numberofrows}, 
y dir=reverse,
y tick label style={major tick length=5pt},
yticklabels from table={data.csv}{[index]2},
nodes near coords, nodes near coords align=horizontal,
%point meta=explicit symbolic
]

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

\addplot [draw,fill=blue!60!white, discard if not={Bike}{Yfoil}] 
        table [y expr=\coordindex, x index=0, col sep=comma]{data.csv};

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

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

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

\end{axis}
\end{tikzpicture}

\end{document}

2
  • Thanks for the solution. Works like a charm. Regards, Dave.
    – user50506
    Commented May 26, 2014 at 6:16
  • @user50506: Glad it helps! If you consider your question answered, you can mark it as such by clicking the tick mark next to my answer.
    – Jake
    Commented May 26, 2014 at 11:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .