2

I have this code.

\begin{figure}
\begin{tikzpicture}
\begin{axis}[
    title=Overhead of the monitoring system,
    title style={yshift=1.3ex},
    y tick label style={
      /pgf/number format/precision=2,
      /pgf/number format/fixed,
      /pgf/number format/fixed zerofill,},
    ymin=0,
    xtick={1,2},xticklabels={Monitored,Non-monitored},
    enlarge x limits=0.15,
    ylabel=Duration in seconds,
    ybar,
    bar width=25pt,
]
\addplot+[ 
    error bars/.cd,
    y dir=both,
    y explicit
]
table [y error=error] {
    x   y           error       label
    1   113.68      1.64        1
    2   110.41      1.72        2
};
\end{axis}
\end{tikzpicture}
\caption{Overhead introduced by monitoring and storing the traces versus non-monitoring. As we can see the latency is minimum }
\label{fig:ovmonitoring}
\end{figure}

The result is

enter image description here

However I want to have a different color for the bar on the right (label=2). I have seen examples in the documentation with scatter plots but I cannot find anything with bars. How can I color each bar depending on the label?

2
  • 3
    you can try this one : tex.stackexchange.com/questions/132081/…
    – percusse
    Dec 16, 2016 at 11:12
  • Not quite because in this case doesn't depend on the height?. Maybe I'm missing something on the code you posted. I'm not an expert in pgf plot.
    – Brandon
    Dec 16, 2016 at 13:51

1 Answer 1

1

If you don't mind to draw as many \addplot commands as you have labels then you can do the following. (For more details on how the solution works, please have a look at the comments in the code.)

% used PGFPlots v1.14
% (inspired by <http://tex.stackexchange.com/a/58563/95441>)
    % store data in a file
    % (currently this solution doesn't seem to work when the table is
    %  either given to the `\addplot' command directly or if it is stored
    %  with `\pgfplotstableread`)
    \begin{filecontents}{test.txt}
        x   y           error       label
        1   113.68      1.64        1
        2   110.41      1.72        2
    \end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.3,
        % create filter that discards all entries from the table row
        % given as first argument of this style with the value not equal
        % to the second argument of this style
        discard if not/.style 2 args={
            % suppress LOG messages about the filtered points
            filter discard warning=false,
            x filter/.code={
                \edef\tempa{\thisrow{#1}}
                \edef\tempb{#2}
                \ifx\tempa\tempb
                \else
                    \def\pgfmathresult{inf}
                \fi
            },
        },
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            title=Overhead of the monitoring system,
            ymin=0,
            xtick={1,...,2},
            xticklabels={
                Monitored,
                Non-monitored,
            },
            enlarge x limits=0.15,
            ylabel=Duration in seconds,
            ybar,
            bar width=25pt,
            % don't shift the bars
            bar shift=0pt,
        ]
            % then cycle through each row of the data table and plot it
            % while using the created filter `discard if not'
            \pgfplotsinvokeforeach {1,...,2}{
                \addplot+ [
                    discard if not={label}{#1},
                    error bars/.cd,
                        y dir=both,
                        y explicit,
                ] table [
                    x=x,
                    y=y,
                    y error=error,
                ]{test.txt};
            }
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

You must log in to answer this question.

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