为了显示敏感性分析,我想生成一个龙卷风情节。在图中,将特定的基准值与模型中更改参数后产生的值进行比较。我从这个开始例子。通过这个我能够得到这个 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}