4

I'm trying to plot a bar chart with pgfplots, with two data series, and two separate y-axes (one on the left, one on the right).

In order to have different axes, it seems that I need to put each \addplot in its own axis environment (see code below). However, when I do this, the bars are drawn on top of each other and I can't separate them:

The bars are stacked on top of each other, and should be separate

If the \addplot commands are in the same axis, the bars are grouped correctly, but then I can't use a second y-axis.

This question seemed promising, but all the plots there are drawn in the first axis environment

Here's my code:

\documentclass{minimal}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{width=9cm,compat=1.5.1}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    ybar,
    symbolic x coords={(a), (b), (c), (d), (e), (f), (g), (h), (i)},
    axis y line*=left,
    axis x line=none,
    ymin=0, ymax=6,
    ylabel=data set 1,
    enlarge x limits=0.2,
    xtick=data]
    \addplot[mark=*,draw=black,fill=cyan,error bars/.cd, y dir=both, y explicit, error bar style={line width=1pt,solid, black}] coordinates {
        ((a),2.208) +- (0.004, 0.004)
        ((b),2.394) +- (0.028, 0.028) 
        ((c), 2.452) +- (0.040, 0.040)
        ((d), 4.000) +- (0.757, 0.757)
        ((e), 2.561) +- (0.245, 0.245)
        ((f), 2.600) +- (0.423, 0.423)
        ((g), 2.731) +- (0.267, 0.267)
        ((h), 2.840) +- (0.207, 0.207)
        ((i), 2.000) +- (0.146, 0.146)
    };
\end{axis} % start a new axis for the second data set
\begin{axis}[
    ybar,
    symbolic x coords={(a), (b), (c), (d), (e), (f), (g), (h), (i)},
    axis y line*=right,
    ymin=0, ymax=5,
    ylabel=data set 2,
    enlarge x limits=0.2]
\addplot[mark=*,fill=red!50,error bars/.cd, y dir=both, y explicit, error bar style={line width=1pt,solid, black}] coordinates {
        ((a),2.21) +- (0.004, 0.004)
        ((b),1.394) +- (0.028, 0.028) 
        ((c), 3.452) +- (0.040, 0.040)
        ((d), 2.346) +- (0.757, 0.757)
        ((e), 1.561) +- (0.245, 0.245)
        ((f), 1.3300) +- (0.423, 0.423)
        ((g), 1.931) +- (0.267, 0.267)
        ((h), 2.820) +- (0.207, 0.207)
        ((i), 3.00) +- (0.146, 0.146)
    };
\end{axis}
\end{tikzpicture}

\end{document}
  • 1
    Welcome to TeX-SE! Could you please describe more clearly what your target output is? And as for "In order to have different axes, it seems that I need to put each \addplot in its own axis environment (see code below)": no, you can have just an empty axis with some appropriate xmin, xmax, ymin and ymax. – user121799 Mar 11 '19 at 18:33
  • @marmot My target output is like the chart in the image, but with the 2 bars for each category next to each other, not drawn on top of each other. Regarding the empty axis environment: I understand that I can do that and it'll draw the second axis, but I want the second set of data to be plotted according to that axis. – Joe Malt Mar 11 '19 at 20:15
2

Perhaps you figured this out, or time has passed so you don't need it, but anyways: With the appropriate bar width and bar shift, as well as a corresponding xshift for the markers, you can get this:

enter image description here

\documentclass{article} % don't use minimal
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{width=9cm,compat=1.5.1}

\begin{document}

\begin{tikzpicture}[
   declare function={
    barW=8pt; % width of bars
    barShift=barW/2; % bar shift
  }
]
\begin{axis}[
    ybar,
    bar width=barW, % added
    bar shift=-barShift, % added
    symbolic x coords={(a), (b), (c), (d), (e), (f), (g), (h), (i)},
    axis y line*=left,
    axis x line=none,
    ymin=0, ymax=6,
    ylabel=data set 1,
    enlarge x limits=0.1,
    xtick=data
 ]
    \addplot[mark=*,
             mark options={xshift=-barShift}, % <-- added
             draw=black,
             fill=cyan,
             error bars/.cd,
               y dir=both,
               y explicit,
               error bar style={line width=1pt,solid, black}
    ] coordinates {
        ((a),2.208) +- (0.004, 0.004)
        ((b),2.394) +- (0.028, 0.028) 
        ((c), 2.452) +- (0.040, 0.040)
        ((d), 4.000) +- (0.757, 0.757)
        ((e), 2.561) +- (0.245, 0.245)
        ((f), 2.600) +- (0.423, 0.423)
        ((g), 2.731) +- (0.267, 0.267)
        ((h), 2.840) +- (0.207, 0.207)
        ((i), 2.000) +- (0.146, 0.146)
    };
\end{axis} % start a new axis for the second data set
\begin{axis}[
    ybar,
    bar width=barW,
    bar shift=barShift,
    symbolic x coords={(a), (b), (c), (d), (e), (f), (g), (h), (i)},
    axis y line*=right,
    ymin=0, ymax=5,
    ylabel=data set 2,
    enlarge x limits=0.1
]
\addplot[mark=*,
         mark options={xshift=barShift}, % <-- added
         fill=red!50,
         error bars/.cd,
           y dir=both,
           y explicit,
           error bar style={line width=1pt,solid, black}
] coordinates {
        ((a),2.21) +- (0.004, 0.004)
        ((b),1.394) +- (0.028, 0.028) 
        ((c), 3.452) +- (0.040, 0.040)
        ((d), 2.346) +- (0.757, 0.757)
        ((e), 1.561) +- (0.245, 0.245)
        ((f), 1.3300) +- (0.423, 0.423)
        ((g), 1.931) +- (0.267, 0.267)
        ((h), 2.820) +- (0.207, 0.207)
        ((i), 3.00) +- (0.146, 0.146)
    };
\end{axis}
\end{tikzpicture}

\end{document}
| improve this answer | |

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.