2

I have multiple machines on which I did benchmarks. Now I wan to use pgfplots to show multiple y values (the variance in the measurement) for a single x value (a benchmark parameter) within a single graph. Each machine should get its own "column" in the plot, similar to the effect ybar produces with multiple addplot commands.

In other questions I already found valuable hints but the legend is broken by the manual mark offset (xshift) as it is applied there as well.

Here's a MWE:

\documentclass[border=1mm]{standalone}
\usepackage{pgfplotstable}

\begin{document}

\pgfplotstableread{
  A B   C
  foo   1   2.4
  foo   1   2.1
  foo   1   2.6
  foo   1   2.3
}\dataA

\pgfplotstableread{
  A B   C
  bar   1   2.9
  bar   1   2.2
  bar   1   2.5
  bar   1   2.1
}\dataB

\begin{tikzpicture}
  \begin{axis}[only marks, ybar, symbolic x coords={1}, xtick={1}]

    \foreach \id / \offs / \tab in {foo/-4/\noexpand\dataA, bar/4/\noexpand\dataB} {
     \edef\temp{\noexpand\addplot+ [every mark/.append style={xshift=\offs pt}] table[x=B, y=C] {\tab};
      \noexpand\addlegendentry{\id}
     }

     \temp
    }

  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

I already tried to compensate for the xoffset by adding [every mark/.append style={xshift=-\offs pt}] to the addlegendentry command, but without any effect.

How can I get the desired "column effect" in the plot without messing up the legend?

5
  • legend style={every mark/.append style={xshift=+0pt}} seems to disable the shifting … – Qrrbrbirlbel May 9 '13 at 13:18
  • @Qrrbrbirlbel This does not work for me. If I add your code to axis it disables the shifting for the plotted points as well. I have no idea why it does this as the style should only be applied to the legend... – TableKing May 9 '13 at 13:51
  • You’re right, I missed that. A hack-ish solution I found previously was legend style={legend image post style={/tikz/xshift/.style=}} which simply disables the xshift key for the legend images. – Qrrbrbirlbel May 9 '13 at 14:19
  • Perfect. Thanks a lot! BTW, the shorter version legend image post style={xshift/.style=} does the same job for me. – TableKing May 9 '13 at 14:47
  • @Qrrbrbirlbel an answer? – cmhughes May 9 '13 at 15:47
2

The key legend image post style appends to the every legend image post style which is a style that only affects the small legend images (not the legend or the text).

By issuing /tikz/xshift/.style= we simply disable the xshift key.

Code

\documentclass[border=1mm]{standalone}
\usepackage{pgfplotstable}

\pgfplotstableread{
  A B   C
  foo   1   2.4
  foo   1   2.1
  foo   1   2.6
  foo   1   2.3
}\dataA

\pgfplotstableread{
  A B   C
  bar   1   2.9
  bar   1   2.2
  bar   1   2.5
  bar   1   2.1
}\dataB

\begin{document}
\begin{tikzpicture}
  \begin{axis}[only marks, ybar, symbolic x coords={1}, xtick={1}, 
    legend image post style={/tikz/xshift/.style=}
  ]
    \foreach \id / \offs / \tab in {foo/-4/\noexpand\dataA, bar/4/\noexpand\dataB} {
      \edef\temp{\noexpand\addplot+ [every mark/.append style={xshift=\offs pt}] table[x=B, y=C] {\tab};
        \noexpand\addlegendentry{\id}
      }
      \temp
    }
 \end{axis}
\end{tikzpicture}
\end{document}

Output

enter image description here

Your Answer

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

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