為了顯示敏感度分析,我想產生一個龍捲風圖。在圖中,將特定的基本情況值與更改模型中的參數後產生的值進行比較。我從這個開始例子。這樣我就可以得到這個 MWE:
\usepackage{pgf}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{intersections,backgrounds}
\usetikzlibrary{decorations.pathmorphing}
\pgfplotsset{
TornadoPlot/.style={% Overall settings
width = 1\textwidth,
line width=1pt,
tick style={line width=0.8pt},
xmin = -50, xmax = 50,
xtick = {-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50},
xticklabels = {-50, -40, -30, -20, -10, 0, 10, 20, 30, 40, 50},
xmajorgrids = true,
minor x tick num = 1,
tick align = outside,
xtick pos = left, ytick pos = left,
xbar, bar shift=0pt,
height = 7cm,
enlarge y limits = 0.1, ytick = {0,1,2,3,4,5,6},
nodes near coords={
\pgfkeys{/pgf/fpu=true}%
\pgfmathparse{\pgfplotspointmeta}%
\pgfmathprintnumber{\pgfmathresult}
},
nodes near coords align = {horizontal},
before end axis/.code={
\draw ({rel axis cs:0,0}-|{axis cs:0,0}) -- ({rel axis cs:0,1}-|{axis cs:0,0});
}
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[TornadoPlot, legend style={at={(0.975,0.25)}}]
\addplot[fill=green!40] coordinates{(-3.5,0) (-16,1) (-17.5,2) (-19,3) (-34,4) (-27,5) (-22,6)};
\addplot[fill=red!40] coordinates{(4,0) (19,1) (12.5,2) (28.5,3) (34,4) (82,5) (38,6)};
\legend{upside, downside}
\end{axis}
\end{tikzpicture}
\end{document}
然而,其中一條條 (82, 5) 的 x 值為正,超出了繪圖域的範圍。我可以限制該條的長度並強制中斷,同時仍顯示 x 的原始值嗎?我見過例子對於垂直長條圖,但到目前為止我還無法在我的龍捲風圖中重現這一點。
答案1
是的你可以。有關如何執行此操作的詳細信息,請查看程式碼中的註釋。
% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
TornadoPlot/.style={% Overall settings
width=\textwidth,
height=7cm,
xmin=-50,
xmax=50,
line width=1pt,
tick style={line width=0.8pt},
xmajorgrids=true,
minor x tick num=1,
tick align=outside,
tick pos=left,
xbar,
bar shift=0pt,
enlarge y limits=0.1,
ytick distance=1,
% -----------------------------------------------------------------
% this is the solution to your problem
% -----------------------------------------------------------------
% we want the *original* x value and store it in a macro ...
visualization depends on={rawx \as \rawx},
% ... which value should be shown in the `nodes near coords'
nodes near coords={%
\pgfmathprintnumber{\rawx}%
},
% -----------------------------------------------------------------
before end axis/.code={
\draw ({rel axis cs:0,0} -| {axis cs:0,0})
-- ({rel axis cs:0,1} -| {axis cs:0,0})
;
},
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
TornadoPlot,
legend style={at={(0.975,0.25)}},
% and we want to limit the range of the bars to the axis limits
restrict x to domain*={
\pgfkeysvalueof{/pgfplots/xmin}:\pgfkeysvalueof{/pgfplots/xmax}
},
]
\addplot [fill=green!40] coordinates
{(-3.5,0) (-16,1) (-17.5,2) (-19,3) (-34,4) (-27,5) (-22,6)};
\addplot [fill=red!40] coordinates
{(4,0) (19,1) (12.5,2) (28.5,3) (34,4) (82,5) (38,6)};
\legend{upside, downside}
\end{axis}
\end{tikzpicture}
\end{document}