1

x axis labels I would like to recreateDear community,

I am trying to build a stacked y bar diagram using pgfplots version 1.16. The stacked bars are no problem but the labeling of the x axis. I am trying to rebuild the following diagram with its major and minor labels. I could not find anything like this in the pgfplots manual. Maybe somebody has an idea how this might work?

Thank you very much in advance for your help!

\documentclass[12pt,a4paper]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}%Diagramme
\pgfplotsset{compat=1.16}

\begin{document} \begin{figure} \begin{tikzpicture} \begin{axis}[ ybar stacked, width=0.6\textwidth, height=8cm, symbolic x coords={Kessel in Betrieb (K), Kessel in Betrieb (Ö), Kessel in Betrieb (G), Neuer Kessel (K), Neuer Kessel (Ö), Neuer Kessel (G), Sonderregionen (K), Sonderregionen (Ö), Sonderregionen (G), Jin Jing Da Kessel, Lin Jing Kessel}, xtick={Kessel in Betrieb (K), Kessel in Betrieb (Ö), Kessel in Betrieb (G), Neuer Kessel (K), Neuer Kessel (Ö), Neuer Kessel (G), Sonderregionen (K), Sonderregionen (Ö), Sonderregionen (G), Jin Jing Da Kessel, Lin Jing Kessel}, xticklabel style={rotate=70, anchor=east}, scaled ticks=false, tick label style={/pgf/number format/fixed}, legend pos=outer north east] \addplot coordinates {(Kessel in Betrieb (K), 85) (Kessel in Betrieb (Ö), 60) (Kessel in Betrieb (G), 35) (Neuer Kessel (K), 50) (Neuer Kessel (Ö), 30) (Neuer Kessel (G), 20) (Sonderregionen (K), 30) (Sonderregionen (Ö), 30) (Sonderregionen (G),20) (Jin Jing Da Kessel, 0) (Lin Jing Kessel, 10)}; \addlegendentry{PM} \addplot coordinates {(Kessel in Betrieb (K), 400) (Kessel in Betrieb (Ö), 300) (Kessel in Betrieb (G), 100) (Neuer Kessel (K), 300) (Neuer Kessel (Ö), 200) (Neuer Kessel (G), 50) (Sonderregionen (K), 200) (Sonderregionen (Ö), 100) (Sonderregionen (G), 50) (Jin Jing Da Kessel, 3.5) (Lin Jing Kessel, 0)}; \addlegendentry{SO2} \addplot coordinates {(Kessel in Betrieb (K), 400) (Kessel in Betrieb (Ö), 400) (Kessel in Betrieb (G), 100) (Neuer Kessel (K), 400) (Neuer Kessel (Ö), 300) (Neuer Kessel (G), 250) (Sonderregionen (K), 200) (Sonderregionen (Ö), 100) (Sonderregionen (G), 150) (Jin Jing Da Kessel, 100) (Lin Jing Kessel, 30)}; \addlegendentry{NOx} \end{axis} \end{tikzpicture} \end{figure} \end{document}

Stefan Pinnow
  • 29,535
iTzOle
  • 13

1 Answers1

1

I adjusted the answer https://tex.stackexchange.com/a/342717/95441 to work for stacked bar charts. with that you should be able to adapt your code. Good luck!

% -----------------------------------------------------------------------------
% based on <https://tex.stackexchange.com/a/342717/95441>
% used PGFPlots v1.18.1
% -----------------------------------------------------------------------------
% restructured data file:
% - 'n' values are sorted ascending
% - added column 'z' to know which data belong to which measurement
% - added a dummy line at the end of each data set to produce an empty entry
%   for the separation of the blocks
% - put all data in one file
\begin{filecontents}{data.txt}
z   n   pFA pFB
10  2   6   1
10  4   7   1
10  5   8   4
10  6   9   7
10  8   10  9
20  {}  0   0
20  2   9   4
20  4   12  6
20  5   14  3
20  6   15  8
20  8   18  5
30  {}  0   0
30  2   11  5
30  4   17  5
30  5   19  5
30  6   21  6
30  8   24  8
30  {}  0   0
40  2   13  3
40  4   19  5
40  5   21  18
40  6   24  8
40  8   28  10
40  {}  0   0
50  2   15  4
50  4   21  12
50  5   24  4
50  6   27  9
50  8   30  10
\end{filecontents}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % use this `compat` level or higher to make use of the advanced axis label placement
    \pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
    Label/.style={
        label={[label distance=2ex]below:#1}
    },
]
    \begin{axis}[
        ybar stacked,
        footnotesize,
        % set the `width' of the plot to the maximum length ...
        width=\textwidth,
        % ... and use half this length for the `height'
        height=0.5\textwidth,
        ymin=0,
        % use `data' for the positioning of the `xticks' ...
        xtick=data,
        % ... and use table data for labeling the `xticks'
        xticklabels from table={data.txt}{n},
        % add extra ticks "at the empty entries to add the vertical lines
        extra x ticks={5,11,17,23},
        % this ticks shouldn't be labeled ...
        extra x tick labels={},
        % ... but grid lines should be drawn without the tick lines
        extra x tick style={
            grid=major,
            major tick length=0pt,
        },
        xlabel={X Axis Label},
        ylabel={Y Axis Label},
        % because of the category labels, shift the `xlabel' a bit down
        xlabel style={
            yshift=-4ex,
        },
        legend pos=north west,
        legend entries={
            Algorithm A,
            Algorithm B,
        },
        area legend,
        % adjust `bar width' so it fits your needs ...
        bar width=8pt,
        % ... and with that you also have to adjust the x limits
        enlarge x limits={abs=1},
        % set `clip mode' to `individual' so the category labels aren't clipped away
        clip mode=individual,
    ]
    \addplot table [
        % use just the `coordindex' as x coordinate,
        % the correct labeling is done with `xticklabels from table'
        x expr=\coordindex,
        y=pFA,
    ] {data.txt};

    \addplot table [
        x expr=\coordindex,
        y=pFB,
    ] {data.txt};

    \node [Label=10 Flows]
        at (axis cs:2,\pgfkeysvalueof{/pgfplots/ymin}) {};
    \node [Label=20 Flows]
        at (axis cs:8,\pgfkeysvalueof{/pgfplots/ymin}) {};
    \node [Label=30 Flows]
        at (axis cs:14,\pgfkeysvalueof{/pgfplots/ymin}) {};
    \node [Label=40 Flows]
        at (axis cs:20,\pgfkeysvalueof{/pgfplots/ymin}) {};
    \node [Label=50 Flows]
        at (axis cs:26,\pgfkeysvalueof{/pgfplots/ymin}) {};

\end{axis}

\end{tikzpicture} \end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535