我正在尝试使用 tikz 和/或 pgfplots 复制以下内容:
我尝试改编代码具有负坐标的 ybar 堆叠图,但条形图似乎与轴绑定(即,我无法输出任何类似于图中红色部分的内容)。
非常感谢您的帮助!
编辑:添加了示例代码。我希望底部的橙色部分(在 -4 处)消失,并能够添加虚线垂直线。
\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar stacked,
bar width=10pt,
axis lines*=middle,
axis on top=false,
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}
\end{filecontents*}
答案1
使用 TiKz,此代码的输出可能如下:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node at (0,0) [draw, rectangle, fill=blue!20, minimum height=0.5cm, minimum width=5cm] (rect1) {};
\node at (2.5,-1)[draw, rectangle, fill=red, minimum height=0.5cm, minimum width=4cm] (rect2) {};
\draw [ dashed,red] (rect2.west)--++(90:1.25);
\draw [ dashed, red] (rect2.north)--(rect1.east);
\foreach \x/\alp in {-1.25/A,1.5/B,3.5/C,5.5/(a)}{
\node at (\x,-0.5) {\small \alp};}
\foreach \x/\alp in {-2.5/T1,0.5/T2,2.5/T3,5/T4}{
\node at (\x,-1.75) {\small \alp};}
\node at (1,-4) [draw, rectangle, fill=blue!20, minimum height=0.5cm, minimum width=7cm] (rect3) {};
\foreach \x/\alp in {-1.25/A,1.5/A,3.5/A}{
\node at (\x,-3.25) {\small \alp};}
\foreach \x/\alp in {-2.5/T1,0.075/T2,2.5/T3,4.5/T4}{
\node at (\x,-4.75) {\small \alp};}
\node at (5.5,-4) {\small (b)};
% \filldraw (
\draw [dashed] (0,-4.25)--++(90:1.5);
\draw [dashed] (2.5,-4.25)--++(90:1.5);
![enter image description here][1]
\end{tikzpicture}
\end{document}
输出: