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 11 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}

\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,...,\pgfplotsretval}, 
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 at 15:15

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.