2

Take the following graph:

\documentclass[12pt,border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}

\begin{document}

\begin{tikzpicture}
    \begin{groupplot}[
        group style={group size=2 by 1,horizontal sep=5cm},
        title style = {align=center},
        xbar,
        point meta=explicit symbolic,
        nodes near coords,
        nodes near coords align = {right},
        y axis line style = { opacity = 0 },
        axis x line       = none,
        tickwidth         = 0pt,
        y dir = reverse
    ]
    \nextgroupplot[
        ytick={1,2},
        yticklabels={A,B},      
        title={First graph}
    ]
    \addplot coordinates {
        (20,1) [20]
        (10,2) [10]
    };
    \nextgroupplot[
        title={Second graph},
        ytick={1,2},
        yticklabels={A,Looooooooooong B}
    ]
    \addplot coordinates {
            (20,1) [20]
            (30,2) [30]
    };
    \end{groupplot}
\end{tikzpicture}

\end{document}

first graph

This graph is confusing. To be able to compare the graphs the bar '20' in the first graph should be as long as the bar '20' in the second graph. Also, the bar '30' in the second graph should be 3 times as long as the bar '10' in the first graph.

How can I fix this problem?

I have tried a few fixes found in seemingly related threads, just as this one or this one, but they do not seem to work in my case. I think I simply do not understand why scaling works the way it does in pgfplots.

For example I thought that adding x=1cm in the options of \begin{groupplot} would fix my problem, my reasonning being that it would tell pgfplots that 1 unit has to be always 1cm whatever the graph. But it just makes the problem worse: enter image description here

I then thought that maybe I should rescale the x-axis of the second plot. Since the largest number in the second plot, 30, is 50% larger than the largest number in the first plot, 20, maybe adding x post scale = 1.5 in the second graph would help? But no: enter image description here

I believe the problem might come from the fact that I have a long label in the second graph, but adding the scale only axis option does not fix it.

I do not even need an automated solution, simply that 10 in whatever graph takes the same space.

darpich
  • 319

1 Answers1

1

All you need to do is to add xmin=0 along with x=2mm,y=2cm, say.

\documentclass[12pt,border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}

\begin{document}

\begin{tikzpicture}
    \begin{groupplot}[x=2mm,y=2cm,
        group style={group size=2 by 1,horizontal sep=5cm},
        title style = {align=center},
        xbar,xmin=0,
        point meta=explicit symbolic,
        nodes near coords,
        nodes near coords align = {right},
        y axis line style = { opacity = 0 },
        axis x line       = none,
        tickwidth         = 0pt,
        y dir = reverse
    ]
    \nextgroupplot[
        ytick={1,2},
        yticklabels={A,B},      
        title={First graph}
    ]
    \addplot coordinates {
        (20,1) [20]
        (10,2) [10]
    };
    \nextgroupplot[
        title={Second graph},
        ytick={1,2},
        yticklabels={A,Looooooooooong B}
    ]
    \addplot coordinates {
            (20,1) [20]
            (30,2) [30]
    };
    \end{groupplot}
\end{tikzpicture}

\end{document}

enter image description here

  • @darpich You're right. I forgot to add the fixed dimensions, which you already did in your nice question. (When testing I was mainly confused about the xmin thingy.) –  Oct 24 '18 at 09:38
  • Perfect! The solution is so simple but I couldn't figure it out. Specifying xmin and xmax also gets the desired result. What is the rationale for this behaviour? – darpich Oct 24 '18 at 09:39
  • @darpich I do not know the precise rationale except that pgfplots tries to survey the plots first and then implement some transformations to make the result have the right size. I actually did not expect that it would cut the bars, but then the bars looked as they were cut, and after adding xmin=0 they were no longer. That is to say that I also find this behavior a bit surprising. –  Oct 24 '18 at 14:04