
正と負の座標が混在する 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 単位 (座標と同じ) の長さにしたいのですが、左の 2 つのバーは緑のバーの後ろに隠れています。これに対する解決策はありますか? (\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
環境で作成することです。両方の軸の寸法とバーの幅が同じであることを確認する必要があり、2 番目の軸の軸線を削除する必要があるため、最適ではありませんが、目的の効果は得られます。
答え3
私たちも同じ問題を抱えていました。
私たちが考え出した解決策は、比較的非侵入的です。LaTeX コードは同じままで、特別なタグは追加されませんが、負の値の x 座標は少し変わります。
デフォルトではすべての x 座標が個別に積み重ねられるため、正の値は 1、2、3 などにプロットされ、負の値は 0.999、1.999、2.999 などにプロットされます。
人間の目には、直接重なっているように見えます (重なっていない場合は、さらに 9 を追加します)。ただし、積み重ねる目的で、ラテックスはそれらを別々に積み重ねます。
(ソース:ホームページ)
以下は .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
. . .