4

I'm trying to create a tikzpicture, composed of 2 separate diagrams. I'm using the \usepgfplotslibrary{groupplots} package for this. All is going fine, but I cannot seem to get the bars to a more central position (see image: they're currently at the very corners of the plot).

Current version

This is a minimal working example:

\documentclass{article}

\usepackage{pgfplots} \usepgfplotslibrary{groupplots} \usepackage{tikz} \usetikzlibrary{matrix} \pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture} \begin{groupplot}[ group style={group size= 2 by 1}, height=8cm, width=5cm, ymax=100, ymin=0, ybar, xtick=data, x tick label style={rotate=45,anchor=east} ] \nextgroupplot[title=A,legend to name=zelda, symbolic x coords={Class3, Class4}, bar width=9pt] \addplot+[ybar, draw=red, fill=red] plot coordinates { (Class3, 60.83) (Class4, 54.83) }; \addlegendentry{(Both) correct}; \addplot+[ybar, draw=blue, fill=blue] plot coordinates { (Class3, 37.00) (Class4, 42.17) }; \addlegendentry{One correct}; \coordinate (top) at (rel axis cs:0,1);% coordinate at top of the first plot \nextgroupplot[title=B, symbolic x coords={Class1, Class2}, bar width=9pt] \addplot+[ybar, draw=red, fill=red] plot coordinates { (Class1,89.00) (Class2,87.00) }; \coordinate (bot) at (rel axis cs:1,0);% coordinate at bottom of the last plot \end{groupplot} \path (top)--(bot) coordinate[midway] (group center); \node[above,rotate=90] at (group center -| current bounding box.west) {Accuracy}; \node[right=1em,inner sep=0pt] at(group center -| current bounding box.east {\pgfplotslegendfromname{zelda}}; \end{tikzpicture}

\end{document}

Any help would be highly appreciated.

Bernard
  • 271,350
Dreana
  • 177

1 Answers1

2

Turns out, enlarge x limits did the trick. This makes it necessary to change the symbolic coords to xtick={1,2}, and then add enlarge x limits=0.5 to the \nextgroupplot section

Full code:

\documentclass{article}

\usepackage{pgfplots} \usepgfplotslibrary{groupplots} \usepackage{tikz} \usetikzlibrary{matrix} \pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture} \begin{groupplot}[ group style={group size= 2 by 1}, height=8cm, width=5cm, ymax=100, ymin=0, ybar, xtick={1,2}, x tick label style={rotate=45,anchor=east} ] \nextgroupplot[title=A,legend to name=zelda, xticklabels={Class3, Class4}, bar width=9pt, enlarge x limits=0.5] \addplot+[ybar, draw=red, fill=red] plot coordinates { (Class3, 60.83) (Class4, 54.83) }; \addlegendentry{(Both) correct}; \addplot+[ybar, draw=blue, fill=blue] plot coordinates { (Class3, 37.00) (Class4, 42.17) }; \addlegendentry{One correct}; \coordinate (top) at (rel axis cs:0,1);% coordinate at top of the first plot \nextgroupplot[title=B, xticklabels={Class1, Class2}, bar width=9pt, enlarge x limits=0.5] \addplot+[ybar, draw=red, fill=red] plot coordinates { (Class1,89.00) (Class2,87.00) }; \coordinate (bot) at (rel axis cs:1,0);% coordinate at bottom of the last plot \end{groupplot} \path (top)--(bot) coordinate[midway] (group center); \node[above,rotate=90] at (group center -| current bounding box.west) {Accuracy}; \node[right=1em,inner sep=0pt] at(group center -| current bounding box.east {\pgfplotslegendfromname{zelda}}; \end{tikzpicture}

\end{document}

final

Dreana
  • 177
  • I get it work without changing symbolic coordinates. What I am missing ? – Hafid Boukhoulda Sep 03 '21 at 14:49
  • Maybe my thinking was too complicated. I first tried this with enlargelimits=0.5 which only showed an effect on the y-axis. Even better if it works without changing the symbolic coords! – Dreana Sep 03 '21 at 14:52