
當使用堆疊在正負座標混合上的 ybar 時,我似乎找不到我想要的答案。
\begin{figure}
\begin{tikzpicture}
\begin{axis}[ybar stacked]
\addplot[green,fill=green] coordinates { (1, 3) (2, 3) (3,3) (4,3) };
\addplot[red,fill=red] coordinates { (1,-4) (2,-4) (3,4) (4,4) };
\end{axis}
\end{tikzpicture}
\end{figure}
我想讓所有紅色條都長 4 個單位(因為它們在座標中),儘管左邊的兩個條隱藏在綠色條後面。有什麼辦法可以解決這個問題嗎? (更改\addplot
上面程式碼中的呼叫順序無法修復此問題。)
答案1
我建議使用ybar stacked
繪圖樣式,但組件水平偏移,這構成了《瀑布圖》。這可以很好地顯示正和負貢獻如何加總到最終值(比將負分量繪製在 y 軸下方更好,這會誤導性地創建更大的總長度)。
若要繪製條之間的連接器,您必須使用 定義一個新標記\pgfdeclareplotmark
。
\documentclass{article}
\usepackage{pgfplots}
\pgfdeclareplotmark{waterfall bridge}{\pgfpathmoveto{\pgfpoint{-13pt}{0pt}}\pgfpathlineto{\pgfpoint{13pt}{0pt}}\pgfusepathqstroke}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
bar width=10pt,
axis lines*=middle,
axis on top=false,
xtick=data,
enlarge y limits=0.2,
after end axis/.code={
\node at ({rel axis cs:0,0}|-{axis cs:0,0}) [anchor=east] {0};
}
]
\addplot[
fill=cyan,
draw=none,
bar shift=-8pt,
mark options={
gray,
thick,
dashed
},
mark=waterfall bridge
] coordinates { (1, 3) (2, 3) (3,3) (4,3) };
\addplot[
fill=orange,
draw=none,
bar shift=8pt
] coordinates { (1,-4) (2,-4) (3,4) (4,4) };
\end{axis}
\end{tikzpicture}
\end{document}
答案2
我也多次遇到這個問題。我的(誠然醜陋的)解決方法是在單獨的環境中創建負條axis
。這是次優的,因為您必須確保兩個軸的尺寸和條形寬度相同,並且必須消除第二個軸上的軸線,但它確實達到了預期的效果。
答案3
我們有同樣的問題。
我們提出的解決方案相對來說是非侵入性的。您的乳膠代碼保持不變,沒有額外的特殊標籤,但您的負值的 x 座標略有變化。
由於預設情況下每個 x 座標都是單獨堆疊的,因此我們將在 1、2、3 等處繪製正值,在 0.999、1.999、2.999 等處繪製負值。
對於人眼來說,它看起來像是直接在彼此之上(如果不是,則添加另一個 9),但為了堆疊的目的,latex 會將它們分開堆疊。
(來源:pnpscada.com)
以下是 .tex 磁貼的程式碼摘錄:
. . .
\begin{axis}[width=20cm, height=9.75cm,
scaled ticks=false,%
%bar width=5pt,
xmin=-0.15,
xtick={1,...,\days},
xmax=\the\value{daysp}.15,
ylabel=energy in \si{kWh},
xlabel = date,
%xlabel= {\yearnow},
%xtick align=center,
%xticklabel style={rotate=90,anchor=east},
grid=major,
legend cell align=left,
%legend entries={baseline,target,trend,low demand--off-peak,low demand--standard,low demand--peak,high demand--off-peak,high demand--standard,high demand--peak},
legend style = {area legend,legend columns = 4,
%legend title = legend,
at={(0.5,-0.12)},
anchor=north},
%legend entries={baseline,target,trend,low demand--off-peak,low demand--standard,low demand--peak,high demand--off-peak,high demand--standard,high demand--peak},
title={{\month} {\yearnow} consumption for \meter},
%title style={at={(0.5,1)}},
ybar stacked,
set layers
]
. . .
\trend ;
\addplot[draw=black, fill=color8, forget plot] table [x=x,y=v8]{Month.csv};
\addplot[draw=black, fill=color1, forget plot] table [x=x,y=v1]{Month.csv};
\addplot[draw=black, fill=color12, forget plot] table [x=x,y=v12]{Month.csv};
\addplot[draw=black, fill=color6, forget plot] table [x=x,y=v6]{Month.csv};
\addplot[draw=black, fill=color9, forget plot] table [x=x,y=v9]{Month.csv};
\addplot[draw=black, fill=color3, forget plot] table [x=x,y=v3]{Month.csv};
. . .
以及類似 Month.csv 檔案的摘錄(與圖片不完全相同,但它顯示了想法)(列以製表符分隔):
x v7 v8 v1 v2 v11 v12 v5 v6 v9 v10 v3 v4 target
. . .
6.25 0 0 0 0 0 0 0 0 0 0 0 0 1682.16
6.5 0 0 0 0 0 0 0 0 0 0 0 0 1682.16
6.999 0 0 -30 0 0 0 0 0 0 0 -54.500 0 1682.16
7 0 0 0 200 0 0 0 0 0 0 0 1190.000 1682.16
7.25 0 0 0 0 0 0 0 0 0 0 0 0 1682.16
7.5 0 0 0 0 0 0 0 0 0 0 0 0 1682.16
7.999 0 0 -50 0 0 0 0 0 0 0 -93.000 0 1682.16
8 0 0 0 400 0 0 0 0 0 0 0 1068.000 1682.16
. . .