我尝试过一段时间使用 ybar 创建这种图表,但它是“ybar 堆叠”和“ybar”的混合体。我该怎么做?
我尝试过这种方法,但没有得到很好的结果:
\begin{tikzpicture}
\begin{axis}[ybar stacked, symbolic x coords={Duracem, Technocem, Alipre, Duracem,
Technocem, Duracem, Technocem, Alipre, Duracem, Technocem, Duracem, Technocem, Alipre,
Duracem, Technocem, Duracem, Technocem, Alipre, Duracem, Technocem, Duracem, Technocem,
Alipre, Duracem, Technocem}]
\addplot coordinates {(Duracem,47.2) (Technocem,39) (Alipre,28) (Duracem,16.2)
(Technocem,15) (Duracem,40) (Technocem,16.4) (Alipre,28) (Duracem,33) (Technocem,15)
(Duracem,89) (Technocem,96)(Alipre,49.6) (Duracem,22.9) (Technocem,15.1) (Duracem,105)
(Technocem,83) (Alipre,47) (Duracem,19.5)(Technocem,24.4)};
\end{axis}
\end{tikzpicture}
图表想要:
答案1
ybar stacked
您可以为此使用正常情节。
对于像这样的复杂图表,使用 以表格形式提供数据是有意义的\pgfplotstableread{< data >}{<\macroname>}
。
为了正确分组,我引入了一个X
包含图的水平位置的列。
可以使用 提供图表的标签xtick=data, xticklabels from table={\datatable}{Name}
。关键xtick data
是确保每个条形都有标签。
为了放置组标签,我定义了一个新的键draw group line={<group column>}{<group value>}{<group label>}{<vertical offset>}{<line extension>}
。当然可以通过使用样式来改进此键,使其使用起来更舒适,但它对于此应用程序来说已经很合适了。
\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\newcounter{groupcount}
\pgfplotsset{
draw group line/.style n args={5}{
after end axis/.append code={
\setcounter{groupcount}{0}
\pgfplotstableforeachcolumnelement{#1}\of\datatable\as\cell{%
\def\temp{#2}
\ifx\temp\cell
\ifnum\thegroupcount=0
\stepcounter{groupcount}
\pgfplotstablegetelem{\pgfplotstablerow}{X}\of\datatable
\coordinate [yshift=#4] (startgroup) at (axis cs:\pgfplotsretval,0);
\else
\pgfplotstablegetelem{\pgfplotstablerow}{X}\of\datatable
\coordinate [yshift=#4] (endgroup) at (axis cs:\pgfplotsretval,0);
\fi
\else
\ifnum\thegroupcount=1
\setcounter{groupcount}{0}
\draw [
shorten >=-#5,
shorten <=-#5
] (startgroup) -- node [anchor=base, yshift=0.5ex] {#3} (endgroup);
\fi
\fi
}
\ifnum\thegroupcount=1
\setcounter{groupcount}{0}
\draw [
shorten >=-#5,
shorten <=-#5
] (startgroup) -- node [anchor=base, yshift=0.5ex] {#3} (endgroup);
\fi
}
}
}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread{
X Gp C1 C2 Name Zn Pb
1 1A 0.2 3 Duracem 47.2 12
2 1A 0.2 3 Technocem 39 11
3 1A 0.2 3 Alipre 28 13
5 1A 0.2 8 Duracem 16.2 12
6 1A 0.2 8 Technocem 15 15
8 1A 0.5 3 Duracem 89 17
9 1A 0.5 3 Technocem 96 19
10 1A 0.5 3 Alipre 49.6 20
12 1A 0.5 8 Duracem 22.9 12
13 1A 0.5 8 Technocem 15.1 11
15 2A 0.2 3 Duracem 105 10
16 2A 0.2 3 Technocem 83 17
17 2A 0.2 3 Alipre 47 20
19 2A 0.2 8 Duracem 19.5 21
20 2A 0.2 8 Technocem 24.4 8
}\datatable
\begin{axis}[
axis lines*=left, ymajorgrids,
width=12cm, height=6cm,
ymin=0,
ybar stacked,
bar width=8pt,
xtick=data,
xticklabels from table={\datatable}{Name},
xticklabel style={rotate=90,xshift=-10ex,anchor=mid east},
draw group line={C2}{3}{3\,\%}{-10ex}{4pt},
draw group line={C2}{8}{8\,\%}{-10ex}{4pt},
draw group line={C1}{0.2}{0.2\,\%}{-7ex}{5pt},
draw group line={C1}{0.5}{0.5\,\%}{-7ex}{5pt},
draw group line={Gp}{1A}{1A}{-4ex}{7pt},
draw group line={Gp}{2A}{2A}{-4ex}{7pt},
after end axis/.append code={
\path [anchor=base east, yshift=0.5ex]
(rel axis cs:0,0) node [yshift=-10ex] {Conc 1}
(rel axis cs:0,0) node [yshift=-7ex] {Conc 2}
(rel axis cs:0,0) node [yshift=-4ex] {Group};
}
]
\addplot table [x=X, y=Zn] {\datatable}; \addlegendentry{Zn}
\addplot table [x=X, y=Pb] {\datatable}; \addlegendentry{Pb}
\end{axis}
\end{tikzpicture}
\end{document}