2

First of all, (as you will clearly see in a moment) I'm a LaTeX noob - getting this far has cost me considerable time, but now I'm stuck for good.

enter image description here

MWE: https://www.overleaf.com/read/gqwjpqnggvcc

I'm trying to adapt this excellent example to use horizontal bars instead of vertical bars. As you can see, my bars start to shift (in very suprising ways, at least for me) and my x-axis scaling seems way off. My goal is that all plots share the same order of entries (Green always on top, turquoise always on bottom etc) and only vary in the contained data. Likewise each x-Axis should be scaled automatically, like in the first of the 3 plots.

Can somebody give me any pointers on where I should start?

Thanks!

\documentclass{article}
\usepackage{lipsum}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.groupplots}

\pgfplotstableread{
Criterion   Rapperswil  Palermo Jeddah
Turquoise       3620        15749       42754
Yellow      2739        15109       42240
Purple      3710        16146       40533
Red           714         9306      28919
Blue        2997          6840  11614
Orange      1589          4992  11786
Green       2122        11154   28747
}\datatable

% Retrieve the number of rows from the datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathsetmacro{\rows}{\pgfplotsretval-1}  

\definecolor{RYB1}{RGB}{141, 211, 199}
\definecolor{RYB2}{RGB}{255, 255, 179}
\definecolor{RYB3}{RGB}{190, 186, 218}
\definecolor{RYB4}{RGB}{251, 128, 114}
\definecolor{RYB5}{RGB}{128, 177, 211}
\definecolor{RYB6}{RGB}{253, 180, 98}
\definecolor{RYB7}{RGB}{179, 222, 105}

\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2},
{RYB3!50!black,fill=RYB3},
{RYB4!50!black,fill=RYB4},
{RYB5!50!black,fill=RYB5},
{RYB6!50!black,fill=RYB6},
{RYB7!50!black,fill=RYB7},
}

\pgfplotsset{
    select row/.style={
        x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
    }
}

\begin{document}

All corresponding bars should be on the same position in each respective chart - green on top, turquoise on the bottom.

\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
\begin{groupplot}
    [
        group style={
            group size=2 by 2,
            % columns=2,
            % rows=2,
            xlabels at=edge bottom,
            ylabels at=edge left,
            horizontal sep=0.05\textwidth, group name=plots
        },
        /pgf/number format/1000 sep={},
        xbar, 
        xmin=0,
        ytick={0,...,\rows},
        yticklabels from table={\datatable}{Criterion},
        xtick pos=bottom,
        % y=0.1pt,
        %
        % ybar=0pt,
        /pgf/bar shift=0pt,scale only axis,
        width=\textwidth/3,
        % yticklabel style={text width=0.035\textwidth, align=right, inner xsep=0pt, xshift=-0.005\textwidth},
        % ylabel=kWh, ylabel style={text height=0.02\textwidth,inner ysep=0pt},
        % enlarge x limits=0.15,
        % ymin=0,
        % xtick=\empty,
        % tickpos=left,
        % scaled y ticks=base 10:-3,
        cycle list name=colorbrewer-RYB,
        legend columns=-1,
        legend style={draw=none, /tikz/every even column/.append style={column sep=5pt}},
        legend image code/.code={%
             \draw[#1] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
        }
    ]

\nextgroupplot[xlabel=Rapperswil, legend to name=grouplegend,]
\pgfplotsinvokeforeach  {0,...,\rows}{
    \addplot table [ y expr=\coordindex, select row=#1, x=Rapperswil] {\datatable};
    \addlegendentry {\pgfplotstablegetelem{#1}{Criterion}\of\datatable \pgfplotsretval}
}

\nextgroupplot[xlabel=Palermo]
\pgfplotsinvokeforeach  {0,...,\rows}{
    \addplot table [x expr=\coordindex, select row=#1, y=Palermo] {\datatable};
}

\nextgroupplot[xlabel=Jeddah]
\pgfplotsinvokeforeach  {0,...,\rows}{
    \addplot table [x expr=\coordindex, select row=#1, y=Jeddah] {\datatable};
}

\end{groupplot}
\end{tikzpicture}
\end{figure}

\end{document}
Hackysack
  • 23
  • 2

1 Answers1

3

I am not exactly sure this is what you want (I had to modify some table options, so please check), but here is what comes when you add /pgf/bar shift=0pt,/pgf/bar width=0.9.

\documentclass{article}
\usepackage{lipsum}
\usepackage{pgfplots,pgfplotstable}
\pgfplotsset{compat=newest}
\usetikzlibrary{pgfplots.groupplots}

\pgfplotstableread{
Criterion   Rapperswil  Palermo Jeddah
Turquoise       3620        15749       42754
Yellow      2739        15109       42240
Purple      3710        16146       40533
Red           714         9306      28919
Blue        2997          6840  11614
Orange      1589          4992  11786
Green       2122        11154   28747
}\datatable

% Retrieve the number of rows from the datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathsetmacro{\rows}{\pgfplotsretval-1}  

\definecolor{RYB1}{RGB}{141, 211, 199}
\definecolor{RYB2}{RGB}{255, 255, 179}
\definecolor{RYB3}{RGB}{190, 186, 218}
\definecolor{RYB4}{RGB}{251, 128, 114}
\definecolor{RYB5}{RGB}{128, 177, 211}
\definecolor{RYB6}{RGB}{253, 180, 98}
\definecolor{RYB7}{RGB}{179, 222, 105}

\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2},
{RYB3!50!black,fill=RYB3},
{RYB4!50!black,fill=RYB4},
{RYB5!50!black,fill=RYB5},
{RYB6!50!black,fill=RYB6},
{RYB7!50!black,fill=RYB7},
}

\pgfplotsset{
    select row/.style={
        x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
    }
}

\begin{document}

All corresponding bars should be on the same position in each respective chart - green on top, turquoise on the bottom.

\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
\begin{groupplot}
    [
        group style={
            group size=2 by 2,
            % columns=2,
            % rows=2,
            xlabels at=edge bottom,
            ylabels at=edge left,
            horizontal sep=0.2\textwidth, group name=plots
        },
        /pgf/number format/1000 sep={},
        xbar,
        /pgf/bar shift=0pt,
        /pgf/bar width=0.9,
        xmin=0,
        ytick={0,...,\rows},
        yticklabels from table={\datatable}{Criterion},
        xtick pos=bottom,
        % y=0.1pt,
        %
        % ybar=0pt,
        %bar shift=0pt,
        scale only axis,
        width=\textwidth/3,
        % yticklabel style={text width=0.035\textwidth, align=right, inner xsep=0pt, xshift=-0.005\textwidth},
        % ylabel=kWh, ylabel style={text height=0.02\textwidth,inner ysep=0pt},
        % enlarge x limits=0.15,
        % ymin=0,
        % xtick=\empty,
        % tickpos=left,
        % scaled y ticks=base 10:-3,
        cycle list name=colorbrewer-RYB,
        legend columns=-1,
        legend style={draw=none, /tikz/every even column/.append style={column sep=5pt}},
        legend image code/.code={%
             \draw[#1] (0cm,-0.1cm) rectangle (0.3cm,0.1cm);
        }
    ]

\nextgroupplot[xlabel=Rapperswil, legend to name=grouplegend,]
\pgfplotsinvokeforeach  {0,...,\rows}{
    \addplot table [ y expr=\coordindex, select row=#1, x=Rapperswil] {\datatable};
    \addlegendentry {\pgfplotstablegetelem{#1}{Criterion}\of\datatable \pgfplotsretval}
}

\nextgroupplot[xlabel=Palermo]
\pgfplotsinvokeforeach  {0,...,\rows}{
    \addplot table [y expr=\coordindex, select row=#1, x=Palermo] {\datatable};
}

\nextgroupplot[xlabel=Jeddah]
\pgfplotsinvokeforeach  {0,...,\rows}{
    \addplot table [y expr=\coordindex, select row=#1, x=Jeddah] {\datatable};
}

\end{groupplot}
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

BambOo
  • 8,801
  • 2
  • 20
  • 47