![Sankey Diagram Tikz 颜色错误](https://linux22.com/image/461289/Sankey%20Diagram%20Tikz%20%E9%A2%9C%E8%89%B2%E9%94%99%E8%AF%AF.png)
我刚刚用 Tikz 创建了一个桑基图,我不知道为什么大多数连接的颜色是正确的,但有些是错误的
\documentclass{standalone}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage{sankey}
\begin{document}
\begin{tikzpicture}[]
\begin{sankeydiagram}[]
\sankeyset{
ratio=1cm/15,
outin steps=4,
start style=arrow,
end style=simple,
draw/.style={draw=white,line width=.4pt},
color/.style={fill/.style={fill=#1,fill opacity=.75}},
shade/.style 2 args={fill/.style={
fill=none,line width=0,
top color=#1,bottom color=#2,
middle color=#1!50!#2!50!white,
fill opacity=.75}},
% colors
@define HTML color/.code args={#1/#2}{\definecolor{#1}{HTML}{#2}},
@define HTML color/.list={
healed/09FF00,same/a6cee3,SI/2CCC26,red/fb9a99,orange/fdbf6f,
violet/cab2d6,yellow/ffff99,blue/1f78b4,improved/B2E7AE
},
% colors of countries
@let country color/.code args={#1/#2}{\colorlet{#1}[rgb]{#2}},
@let country color/.list={
SW/red,
MW/orange,
S/same,
I/improved,
SI/SI,
H/healed,
SWLT/red,
MWLT/orange,
SLT/same,
ILT/improved,
SILT/SI,
HLT/healed},
}
\def\vdist{0mm}
\def\hwidth{7.5em}
\def\hdist{5cm}
\sankeynode{name=SW, quantity=2}
\sankeynode{name=MW, quantity=4, at={[yshift=\vdist]SW.left}, anchor=right}
\sankeynode{name=S, quantity=7, at={[yshift=\vdist]MW.left}, anchor=right}
\sankeynode{name=I, quantity=12, at={[yshift=\vdist]S.left}, anchor=right}
\sankeynode{name=SI, quantity=36, at={[yshift=\vdist]I.left}, anchor=right}
\sankeynode{name=H, quantity=36, at={[yshift=\vdist]SI.left}, anchor=right}
\foreach \country in {SW,MW,S,I,SI,H}{
\sankeystart[color=\country]{\country}
\sankeyadvance[color=\country]{\country}{\hwidth}
}
\sankeyfork{SW}{1/SW-to-SLT,
1/SW-to-SWLT}
\sankeyfork{MW}{2/MW-to-ILT,
1/MW-to-SLT,
1/MW-to-SWLT}
\sankeyfork{S}{ 1/S-to-SILT,
3/S-to-SLT,
2/S-to-MWLT,
1/S-to-SWLT}
\sankeyfork{I}{ 1/I-to-HLT,
3/I-to-SILT,
1/I-to-SLT,
4/I-to-ILT,
1/I-to-MWLT,
2/I-to-SWLT}
\sankeyfork{SI}{5/SI-to-HLT,
11/SI-to-SILT,
6/SI-to-ILT,
5/SI-to-SLT,
6/SI-to-MWLT,
3/SI-to-SWLT}
\sankeyfork{H}{ 14/H-to-HLT,
11/H-to-SILT,
3/H-to-ILT,
3/H-to-SLT,
1/H-to-MWLT,
4/H-to-SWLT}
%\sankeyfork{W}{1/W-to-WLT}
\sankeynode{name=SWLT, quantity=12, at={[xshift=\hdist]SW.right}, anchor=right}
\sankeynode{name=MWLT, quantity=10, at={[yshift=\vdist]SWLT.left}, anchor=right}
\sankeynode{name=SLT, quantity=14, at={[yshift=\vdist]MWLT.left}, anchor=right}
\sankeynode{name=ILT, quantity=15, at={[yshift=\vdist]SLT.left}, anchor=right}
\sankeynode{name=SILT, quantity=26, at={[yshift=\vdist]ILT.left}, anchor=right}
\sankeynode{name=HLT, quantity=20, at={[yshift=\vdist]SILT.left}, anchor=right}
\sankeyfork{SWLT}{ 4/SWLT-from-H,
3/SWLT-from-SI,
2/SWLT-from-I,
1/SWLT-from-MW,
1/SWLT-from-S,
1/SWLT-from-SW}
\sankeyfork{MWLT}{ 1/MWLT-from-H,
6/MWLT-from-SI,
1/MWLT-from-I,
2/MWLT-from-S}
\sankeyfork{SLT}{ 3/SLT-from-H,
5/SLT-from-SI,
1/SLT-from-I,
3/SLT-from-S,
1/SLT-from-MW,
1/SLT-from-SW}
\sankeyfork{ILT}{ 3/ILT-from-H,
6/ILT-from-SI,
4/ILT-from-I,
2/ILT-from-MW}
\sankeyfork{SILT}{ 11/SILT-from-H,
11/SILT-from-SI,
1/SILT-from-S,
3/SILT-from-I}
\sankeyfork{HLT}{ 14/HLT-from-H,
5/HLT-from-SI,
1/HLT-from-I}
\foreach \country in {HLT,SILT,ILT,SLT,MWLT,SWLT}{
\sankeyadvance[color=\country]{\country}{\hwidth}
}
\foreach \startcountry/\countries in {
SW/{SLT,SWLT},
MW/{ILT,SLT,SWLT},
S/{SILT,SLT,MWLT,SWLT},
I/{HLT,SILT,ILT,SLT,MWLT,SWLT},
SI/{HLT,SILT,ILT,SLT,MWLT,SWLT},
H/{HLT,SILT,ILT,SLT,MWLT,SWLT}}
{
\foreach \endcountry in \countries {
\sankeyoutin[shade={\startcountry}{\endcountry}]
{\startcountry-to-\endcountry}{\endcountry-from-\startcountry}
}
}
\foreach \country/\countryname in {
SW/Significantly worse,
MW/Mildly worse,
S/Same,
I/Improved,
SI/Significantly improved,
H/Healed,
SWLT/Significantly worse,
MWLT/Mildly worse,
SLT/Same,
ILT/Improved,
SILT/Significantly improved,
HLT/Healed}
{
\node[anchor=east,inner sep=.20em,font=\tiny]
at (\country) {\countryname\vphantom{Ag}};
}
\end{sankeydiagram}
\node (ST1) at (1.25,-0.5) {Short-term $<$1.5 years};
\node (ST2) at (1.25,-0.9) {($\approx$6-18 months)};
\node (LT1) at (9,-0.5) {Long-term $>$5 years};
\node (LT2) at (9,-0.9) {($\approx$15 years)};
\node[label={[label distance=0.5cm,text depth=-1ex,rotate=90]right: \small Operated patients}] at (-1.5,1) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 100\%}] at (-1.75,6.35) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 75\%}] at (-1.75,4.80) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 50\%}] at (-1.75,3.20) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 25\%}] at (-1.75,1.60) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 0\%}] at (-1.75,-0.05) {};
\end{tikzpicture}
\end{document}
对我来说,这没有任何意义,例如显着改善-->治愈从紫色开始并以绿色结束(相同的解释-->略有恶化从橙色变成绿色而不是从蓝色变成橙色)
...我希望左侧的每个类别都以起始颜色开始
答案1
要获得水平阴影,请使用left color
和right color
而不是top color
和bottom color
...
\documentclass{standalone}
\usepackage{tikz}
\usepackage{verbatim}
\usepackage{sankey}
\begin{document}
\begin{tikzpicture}[]
\begin{sankeydiagram}[]
\sankeyset{
ratio=1cm/15,
outin steps=4,
start style=arrow,
end style=simple,
draw/.style={draw=white,line width=.4pt},
color/.style={fill/.style={fill=#1,fill opacity=.75}},
shade/.style 2 args={fill/.style={
fill=none,line width=0,
left color=#1,right color=#2,
middle color=#1!50!#2!50!white,
fill opacity=.75}},
% colors
@define HTML color/.code args={#1/#2}{\definecolor{#1}{HTML}{#2}},
@define HTML color/.list={
healed/09FF00,same/a6cee3,SI/2CCC26,red/fb9a99,orange/fdbf6f,
violet/cab2d6,yellow/ffff99,blue/1f78b4,improved/B2E7AE
},
% colors of countries
@let country color/.code args={#1/#2}{\colorlet{#1}[rgb]{#2}},
@let country color/.list={
SW/red,
MW/orange,
S/same,
I/improved,
SI/SI,
H/healed,
SWLT/red,
MWLT/orange,
SLT/same,
ILT/improved,
SILT/SI,
HLT/healed},
}
\def\vdist{0mm}
\def\hwidth{7.5em}
\def\hdist{5cm}
\sankeynode{name=SW, quantity=2}
\sankeynode{name=MW, quantity=4, at={[yshift=\vdist]SW.left}, anchor=right}
\sankeynode{name=S, quantity=7, at={[yshift=\vdist]MW.left}, anchor=right}
\sankeynode{name=I, quantity=12, at={[yshift=\vdist]S.left}, anchor=right}
\sankeynode{name=SI, quantity=36, at={[yshift=\vdist]I.left}, anchor=right}
\sankeynode{name=H, quantity=36, at={[yshift=\vdist]SI.left}, anchor=right}
\foreach \country in {SW,MW,S,I,SI,H}{
\sankeystart[color=\country]{\country}
\sankeyadvance[color=\country]{\country}{\hwidth}
}
\sankeyfork{SW}{1/SW-to-SLT,
1/SW-to-SWLT}
\sankeyfork{MW}{2/MW-to-ILT,
1/MW-to-SLT,
1/MW-to-SWLT}
\sankeyfork{S}{ 1/S-to-SILT,
3/S-to-SLT,
2/S-to-MWLT,
1/S-to-SWLT}
\sankeyfork{I}{ 1/I-to-HLT,
3/I-to-SILT,
1/I-to-SLT,
4/I-to-ILT,
1/I-to-MWLT,
2/I-to-SWLT}
\sankeyfork{SI}{5/SI-to-HLT,
11/SI-to-SILT,
6/SI-to-ILT,
5/SI-to-SLT,
6/SI-to-MWLT,
3/SI-to-SWLT}
\sankeyfork{H}{ 14/H-to-HLT,
11/H-to-SILT,
3/H-to-ILT,
3/H-to-SLT,
1/H-to-MWLT,
4/H-to-SWLT}
%\sankeyfork{W}{1/W-to-WLT}
\sankeynode{name=SWLT, quantity=12, at={[xshift=\hdist]SW.right}, anchor=right}
\sankeynode{name=MWLT, quantity=10, at={[yshift=\vdist]SWLT.left}, anchor=right}
\sankeynode{name=SLT, quantity=14, at={[yshift=\vdist]MWLT.left}, anchor=right}
\sankeynode{name=ILT, quantity=15, at={[yshift=\vdist]SLT.left}, anchor=right}
\sankeynode{name=SILT, quantity=26, at={[yshift=\vdist]ILT.left}, anchor=right}
\sankeynode{name=HLT, quantity=20, at={[yshift=\vdist]SILT.left}, anchor=right}
\sankeyfork{SWLT}{ 4/SWLT-from-H,
3/SWLT-from-SI,
2/SWLT-from-I,
1/SWLT-from-MW,
1/SWLT-from-S,
1/SWLT-from-SW}
\sankeyfork{MWLT}{ 1/MWLT-from-H,
6/MWLT-from-SI,
1/MWLT-from-I,
2/MWLT-from-S}
\sankeyfork{SLT}{ 3/SLT-from-H,
5/SLT-from-SI,
1/SLT-from-I,
3/SLT-from-S,
1/SLT-from-MW,
1/SLT-from-SW}
\sankeyfork{ILT}{ 3/ILT-from-H,
6/ILT-from-SI,
4/ILT-from-I,
2/ILT-from-MW}
\sankeyfork{SILT}{ 11/SILT-from-H,
11/SILT-from-SI,
1/SILT-from-S,
3/SILT-from-I}
\sankeyfork{HLT}{ 14/HLT-from-H,
5/HLT-from-SI,
1/HLT-from-I}
\foreach \country in {HLT,SILT,ILT,SLT,MWLT,SWLT}{
\sankeyadvance[color=\country]{\country}{\hwidth}
}
\foreach \startcountry/\countries in {
SW/{SLT,SWLT},
MW/{ILT,SLT,SWLT},
S/{SILT,SLT,MWLT,SWLT},
I/{HLT,SILT,ILT,SLT,MWLT,SWLT},
SI/{HLT,SILT,ILT,SLT,MWLT,SWLT},
H/{HLT,SILT,ILT,SLT,MWLT,SWLT}}
{
\foreach \endcountry in \countries {
\sankeyoutin[shade={\startcountry}{\endcountry}]
{\startcountry-to-\endcountry}{\endcountry-from-\startcountry}
}
}
\foreach \country/\countryname in {
SW/Significantly worse,
MW/Mildly worse,
S/Same,
I/Improved,
SI/Significantly improved,
H/Healed,
SWLT/Significantly worse,
MWLT/Mildly worse,
SLT/Same,
ILT/Improved,
SILT/Significantly improved,
HLT/Healed}
{
\node[anchor=east,inner sep=.20em,font=\tiny]
at (\country) {\countryname\vphantom{Ag}};
}
\end{sankeydiagram}
\node (ST1) at (1.25,-0.5) {Short-term $<$1.5 years};
\node (ST2) at (1.25,-0.9) {($\approx$6-18 months)};
\node (LT1) at (9,-0.5) {Long-term $>$5 years};
\node (LT2) at (9,-0.9) {($\approx$15 years)};
\node[label={[label distance=0.5cm,text depth=-1ex,rotate=90]right: \small Operated patients}] at (-1.5,1) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 100\%}] at (-1.75,6.35) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 75\%}] at (-1.75,4.80) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 50\%}] at (-1.75,3.20) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 25\%}] at (-1.75,1.60) {};
\node[label={[label distance=0.5cm,text depth=-1ex]right:\tiny 0\%}] at (-1.75,-0.05) {};
\end{tikzpicture}
\end{document}