使用 ybar 堆叠图时,我想使用绝对坐标而不是相对值。这可能吗?我知道我可以将数据重新格式化为相对值,但听起来 pgfplots 也可以完成这项工作。我想我真正的问题是我寻求的不是“堆叠”,而只是“叠加”。这存在吗?
下面是将使用绝对坐标的面积图转换为 ybar 堆叠图的示例,以说明我的意思。
\begin{figure}[h]\centering
\begin{tikzpicture}\begin{axis}[area style,xtick=data]
\addplot[red,fill=red] coordinates { (1,8) (2,8) (3,9) (4,10) } \closedcycle;
\addplot[green,fill=green] coordinates { (1,5) (2,7) (3,8) (4,9) } \closedcycle;
\addplot[blue,fill=blue] coordinates { (1,3) (2,5) (3,7) (4,8) } \closedcycle;
\end{axis}\end{tikzpicture}
\caption{Original area plot ordered back to front using absolute values that I want to convert to a stacked bar plot instead.}
\end{figure}
\begin{figure}[h]\centering
\begin{tikzpicture}\begin{axis}[ybar stacked,xtick=data]
\addplot[blue,fill=blue] coordinates { (1,3) (2,5) (3,7) (4,8) };
\addplot[green,fill=green] coordinates { (1,2) (2,2) (3,1) (4,1) };
\addplot[red,fill=red] coordinates { (1,3) (2,1) (3,1) (4,1) };
\end{axis}\end{tikzpicture}
\caption{Stacked using base values and relative values, ordered bottom to top. This is the output I desire, but I had to modify the data to be relative.}
\end{figure}
\begin{figure}[h]\centering
\begin{tikzpicture}\begin{axis}[ybar stacked,xtick=data]
\addplot[red,fill=red] coordinates { (1,8) (2,8) (3,9) (4,10) };
\addplot[green,fill=green] coordinates { (1,5) (2,7) (3,8) (4,9) };
\addplot[blue,fill=blue] coordinates { (1,3) (2,5) (3,7) (4,8) };
\end{axis}\end{tikzpicture}
\caption{Stacked using original absolute data, ordered back to front.}
\end{figure}
作为新用户,我无法创建新的关键字,但我希望它们是:pgfplots、stacked、ybar、absolute、relative。
答案1
您可以通过设置来覆盖条形图bar shift=0pt
。请注意,您还应始终ymin=0
明确设置以确保整个列都可见。
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[h]\centering
\begin{tikzpicture}\begin{axis}[
ybar,
bar shift=0pt,
ymin=0,
xtick=data]
\addplot[red,fill=red] coordinates { (1,8) (2,8) (3,9) (4,10) };
\addplot[green,fill=green] coordinates { (1,5) (2,7) (3,8) (4,9) };
\addplot[blue,fill=blue] coordinates { (1,3) (2,5) (3,7) (4,8) };
\end{axis}\end{tikzpicture}
\caption{Stacked using original absolute data, ordered back to front.}
\end{figure}
\end{document}