我想要绘制类似这样的图: pgfplots:一个图表中的多个(移位)堆叠图
加上这里介绍的分组类型:我如何将“ybar”和“ybar stacked”与 pgfplots 混合?
我像这样将两者合并,并得到了我想要的那种情节,只是我无法使 tightpage 正常工作:
\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage[tightpage,active]{preview}
\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
}
}
}
\makeatletter
\newcommand\resetstackedplots{
\makeatletter
\pgfplots@stacked@isfirstplottrue
\makeatother
\addplot [forget plot,draw=none] table [x=X, y=Zeroes] {\datatable};
}
\makeatother
\pgfplotstableread{
X Gp C1 C2 Name Zn Pb Cu Fe Cr Zeroes
1 1A 0.2 3 Duracem 47.2 12 13 11 15 0
2 1A 0.2 3 Technocem 39 11 15 25 14 0
3 1A 0.2 3 Alipre 28 13 25 11 16 0
5 1A 0.2 8 Duracem 16.2 12 11 16 17 0
6 1A 0.2 8 Technocem 15 15 17 22 19 0
8 1A 0.5 3 Duracem 89 17 16 23 24 0
9 1A 0.5 3 Technocem 96 19 12 15 11 0
10 1A 0.5 3 Alipre 49.6 20 12 17 21 0
12 1A 0.5 8 Duracem 22.9 12 14 17 20 0
13 1A 0.5 8 Technocem 15.1 11 19 21 26 0
15 2A 0.2 3 Duracem 105 10 21 17 27 0
16 2A 0.2 3 Technocem 83 17 23 26 13 0
17 2A 0.2 3 Alipre 47 20 14 29 16 0
19 2A 0.2 8 Duracem 19.5 21 16 28 23 0
20 2A 0.2 8 Technocem 24.4 8 18 19 20 0
}\datatable
\begin{document}
\begin{preview}
\begin{tikzpicture}
\begin{axis}[
axis lines*=left, ymajorgrids,
width=25cm, 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 +[xshift=-.2cm] table [x=X, y=Zn] {\datatable};% \addlegendentry{Zn}
\addplot +[xshift=-.2cm] table [x=X, y=Pb] {\datatable};% \addlegendentry{Pb}
\resetstackedplots
\addplot +[xshift=.2cm]table [x=X, y=Cu] {\datatable};% \addlegendentry{Zn}
\addplot +[xshift=.2cm]table [x=X, y=Fe] {\datatable};% \addlegendentry{Pb}
\addplot +[xshift=.2cm]table [x=X, y=Cr] {\datatable};% \addlegendentry{Zn}
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}
上述代码生成以下输出,图形被剪切。它应该进一步向右和向上延伸。
如何使上述示例中的 Tightpage 功能正常工作?我不明白为什么它会剪掉图形的某些部分。如果我缩小图形的尺寸,图形周围反而会出现不必要的空白。我想制作一个独立的图片,边框位于图形外部,周围没有不必要的空白。
在我之前的所有图中,我都使用过类似的东西,没有任何问题:
\pgfplotsset{width=15cm,compat=1.3} %use to get tight spacing of labels to tickmarks.
%Not enabled by default because it affects the spacing.
\usepackage[tightpage,active]{preview}
\begin{document}
\begin{preview}
\begin{tikzpicture}
\begin{axis}
% the plot here
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}
然后我在“宽度”(或高度)后设置绘图的大小。由于某种原因,tightpage 这次没有起作用。
答案1
如果您在裁剪之前指定了足够大的文本和纸张尺寸,它就会起作用。您的绘图大于默认文本宽度。例如:
\usepackage[a3paper,landscape]{geometry}