我正在尝试绘制热流图(可能不是正确的名称,它更像是桑基图),并且不确定如何拆分主通道,如下所示(忽略背景红色方块,我不想要那个):
以下是我目前得到的信息:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\shade [top color=white, bottom color=red!50] (0, 0) rectangle (6, 1) node [xshift=-3cm, yshift=-0.5cm] {\emph{Hot}};
\draw (0, 0) -- (6, 0);
\shade [top color=blue!50, bottom color=white] (0, -4) rectangle (6, -3) node [xshift=-3cm, yshift=-0.5cm] {\emph{Cold}};
\draw (0, -3) -- (6, -3);
\shade [top color=red!50, bottom color=blue!50] (2.5, 0.02) rectangle (3.5, -3.02);
\draw (2.2, -3.5) to [bend right] (2.5, -3) -- (2.5, 0) to [bend right] (2.2, 0.5);
\draw (3.8, -3.5) to [bend left] (3.5, -3) -- (3.5, 0) to [bend left] (3.8, 0.5);
\draw [-{Triangle[fill=red]}, color=red, line width=0.4em] (3, -0.5) to (3, -2.5);
\end{tikzpicture}
\end{document}
我可以像上例一样手动在子节点之间弯曲,但这非常繁琐和挑剔。有没有更好的方法来重定向线条,同时保持颜色渐变?
答案1
有些地方有点混乱,使用剪辑使得垂直“管道”的弧线末端更薄,但相对简单:
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations,arrows.meta}
\pgfdeclareverticalshading{heat}{100bp}{%
color(0bp)=(white); color(25bp)=(white); color(30bp)=(blue!50);
color(70bp)=(red!50); color(75bp)=(white); color(100bp)=(white)}
\begin{document}
\begin{tikzpicture}[>={Triangle[angle=60:1 2]}]
\path [shading=heat]
(-2,2) rectangle (2,3/2) (-2,-2) rectangle (2,-3/2);
\draw (-2,3/2) -- (2,3/2) (-2,-3/2) -- (2,-3/2);
\scoped{
\clip (-3/2,-7/4) rectangle (3/2,7/4);
\draw [shading=heat]
(3/4,2) -- (-3/4,2) -- (-3/4,7/4) arc (90:0:1/4) -- (-1/2,-3/2)
arc (360:270:1/4) -- (-3/4,-2) -- (1/4,-2) -- (1/4,-7/4)
arc (270:180:1/4) -- (0,-3/2) -- (0,1/4)
arc (180:270:1/2) -- (2,-1/4) -- (2,1/4) -- (3/4,1/4)
arc (270:180:1/4) -- (1/2,3/2) arc (180:90:1/4) -- (3/4,2);
}
\path (-2,2) node {Hot resevoir $T_h$} (-2,-2) node {Cold resevoir $T_c$};
\draw [red!75, line width=1cm/4, ->] (0,3/2) -- (0,1/2);
\draw [red!75, line width=1cm/8, ->] (-1/4,-1/2) -- (-1/4,-3/2);
\draw [blue!75, line width=1cm/8, ->] (1,0) -- (2,0);
\end{tikzpicture}
\end{document}
这是包含有趣装饰用途的版本。
\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{decorations,arrows.meta}
\newcount\pgfalphacount
\pgfdeclaredecoration{alpha-labels}{setup}{
\state{setup}[width=0pt, next state=label,
persistent precomputation={\pgfalphacount=1}]{}
\state{label}[width=\pgfdecoratedinputsegmentlength,
persistent postcomputation={\advance\pgfalphacount by1}]
{\expandafter\pgfcoordinate\expandafter%
{\csname pgffor@alpha\endcsname\pgfalphacount}{\pgfpointorigin}}
}
\pgfdeclareverticalshading{heat}{100bp}{%
color(0bp)=(white); color(25bp)=(white); color(30bp)=(blue!50);
color(70bp)=(red!50); color(75bp)=(white); color(100bp)=(white)}
\begin{document}
\begin{tikzpicture}[>={Triangle[angle=60:1 2]}]
\path [shading=heat, postaction={decoration=alpha-labels, decorate}]
(-2, 2) -- (-2, 3/2) -- (-1/2, 3/2) -- (-1/2, -3/2) -- (-2,-3/2) --
(-2,-2) -- (2,-2) -- (2,-3/2) -- (0,-3/2) -- (0,1/4)
arc (180:270:1/2) -- (3/2,-1/4) -- (3/2,1/4) -- (3/4,1/4)
arc (270:180:1/4) -- (1/2,3/2) -- (2,3/2) -- (2,2) -- cycle;
\draw (b) -- (c) (e) -- (d) (h) -- (i) (p) -- (q);
\draw (c) ++(-1/4,1/4) arc (90:0:1/4) -- (d) arc (360:270:1/4);
\draw (i) ++(1/4,-1/4) arc (270:180:1/4) -- (j) arc (180:270:1/2) -- (l)
(m) -- (n) arc (270:180:1/4) -- (p) arc (180:90:1/4);
\path (-2,2) node {Hot resevoir $T_h$} (-2,-2) node {Cold resevoir $T_c$};
\draw [red!75, line width=1cm/4, ->] (0,3/2) -- (0,1/2);
\draw [red!75, line width=1cm/8, ->] (-1/4,-1/2) -- (-1/4,-3/2);
\draw [blue!75, line width=1cm/8, ->] (1,0) -- (2,0);
\end{tikzpicture}
\end{document}
答案2
我不知道有没有更简单的方法可以保持颜色渐变:
代码:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\shade[top color=white, bottom color=red!50]
(0, 0) rectangle (6, 1);
\draw (0, 0) -- (6, 0);
\shade[top color=blue!50, bottom color=white]
(0, -4) rectangle (6, -3);
\draw (0, -3) -- (6, -3);
\node at (3,0.8)
{\emph{Hot}};
\node at (2.7,-3.8)
{\emph{Cold}};
\fill[draw=none,line width=0pt,shade,top color=red!50, bottom color=blue!50,middle color=blue!50!red!20]
(2.2, -3.5) to [bend right]
(2.5, -3) --
(2.5, 0) to [bend right]
(2.2, 0.5) --
(3.8, 0.5) to [bend right]
(3.5,0) {[rounded corners=8pt] |-
(5.8,-1.2) --
(5.8,-1.7) -|
(3.05,-1.5) } --
(3.05,-3) to[bend right]
(3.35,-3.5) -- cycle;
\fill[white]
(5.5,-1.2) rectangle (5.8,-1.7);
\draw
(2.2, -3.5) to [bend right]
(2.5, -3) --
(2.5, 0) to [bend right]
(2.2, 0.5)
(3.8, 0.5) to [bend right]
(3.5,0) {[rounded corners=8pt] |-
(5.5,-1.2)
(5.5,-1.7) -|
(3.05,-1.5) } --
(3.05,-3) to[bend right]
(3.35,-3.5);
%
\draw [-{Triangle[fill=red,width=6pt]},color=red, line width=0.5em]
(3, 0) -- ++(0pt, -40pt);
\draw [-{Triangle[fill=red,width=6pt]},color=red, line width=0.3em]
(2.75,-2) -- ++(0pt, -25pt);
\draw [-{Triangle[fill=blue,width=6pt]},color=blue, line width=0.3em]
(4,-1.45) -- ++(25pt,0pt);
\end{tikzpicture}
\end{document}