Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am beginner in LaTeX, trying to use it for industrial documents. I wonder if there is a way to change the color of only one bar of the plot below, ie "TestDetails", to illustrate it is a local macro and not a LaTeX keyword. I have been trying to experiment with point meta, but could not find a way.

\documentclass{article}
\usepackage{filecontents} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\pgfplotsset{width=.95\textwidth, compat=newest} 

\begin{document}

\begin{filecontents}{data.csv}
{Occurences}, {keyword}
215, item
159, SI
134, subsubsection
106, TestDetails
58, caption
\end{filecontents}

\pgfplotstableread[col sep=comma]{data.csv}{\datatable}

\begin{tikzpicture}
\begin{axis}[
/pgf/number format/.cd,1000 sep={},
width=0.8\linewidth,height=6cm,
xbar,
xmin=30, xmax=250,
xtick=\empty,
enlarge x limits={value=0.1, upper},
enlarge y limits=0.1,
ytick=data, 
y dir=reverse,
xlabel= {},
y tick label style={major tick length=0pt},
yticklabels from table={\datatable}{[index]1},
nodes near coords, nodes near coords align=horizontal,
]
\addplot [draw,fill=blue!50]table[
y expr=\coordindex,
x index=0]{\datatable};
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this question

1 Answer 1

up vote 13 down vote accepted

You can't change the colour of an individual bar as such, but you can add a second \addplot where you filter out all bars but the one you want to highlight.

Here are two styles discard if and discard if not, which take two arguments: The name of the column in your table and the value that you want to highlight. Note that you'll need to supply the datafile itself to the \addplot, it doesn't work with a table created using \pgfplotstableread, and that you'll need to use xtick={<list>} instead of xtick=data, as otherwise your labels will be out of sync.

\documentclass{standalone}
\usepackage{filecontents} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\pgfplotsset{width=.95\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}
{Occurences}, {keyword}
215, item
159, SI
134, subsubsection
106, TestDetails
58, caption
\end{filecontents}

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

\begin{tikzpicture}
\begin{axis}[
/pgf/number format/.cd,1000 sep={},
width=0.8\linewidth,height=6cm,
xbar,/pgf/bar shift=0pt,
xmin=0, xmax=250,
xtick=\empty,
enlarge x limits={value=0.1, upper},
enlarge y limits=0.1,
ytick={0,...,\numberofrows}, 
y dir=reverse,
xlabel= {},
y tick label style={major tick length=0pt},
yticklabels from table={data.csv}{[index]1},
nodes near coords, nodes near coords align=horizontal
]

\addplot [draw,fill=blue!50,discard if={keyword}{TestDetails}
] table [
    y expr=\coordindex,
    x index=0,col sep=comma
]{data.csv};

\addplot [draw,fill=orange,discard if not={keyword}{TestDetails}] table [
    y expr=\coordindex,
    x index=0,col sep=comma
]{data.csv};
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
1  
+1 I’ve used this to make each bar a specific color. Maybe there’s an easier solution, but it worked for me. –  Gumbo Jan 6 '14 at 15:15
    
@Jake When I compile again the code above, only the upper bar label (item) is created. I suspect that this change is linked to a more recent version of pgfplots, but I cannot find how to overcome this. –  Yves Mar 5 at 20:45
    
@Yves: For some reason, with the current version of PGFPlots, \pgfplotsretval doesn't retain the correct value for long enough. So instead of using \pgfplotsretval directly, you can store the number of rows in a macro using \edef\numberofrows{\pgfplotsretval} and then use \numberofrows in the ytick key. I've edited the answer. –  Jake Mar 8 at 18:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.