我正在尝试使用 pgfplots 版本 1.16 构建堆叠 y 条形图。堆叠条形图没有问题,但 x 轴的标签有问题。我正在尝试重建下图及其主标签和次标签。我在 pgfplots 手册中找不到类似的东西。也许有人知道这是如何工作的?
非常感谢您的帮助!
\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}
答案1
我调整了答案https://tex.stackexchange.com/a/342717/95441适用于堆积条形图。有了它,您应该能够调整您的代码。祝你好运!
% -----------------------------------------------------------------------------
% 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}