![如何在龙卷风图中更改 x-achsis 的基准值?](https://linux22.com/image/473837/%E5%A6%82%E4%BD%95%E5%9C%A8%E9%BE%99%E5%8D%B7%E9%A3%8E%E5%9B%BE%E4%B8%AD%E6%9B%B4%E6%94%B9%20x-achsis%20%E7%9A%84%E5%9F%BA%E5%87%86%E5%80%BC%EF%BC%9F.png)
我正在尝试绘制一个龙卷风图,其中 x 轴上的基值不同于零,在我的情况下是 5.43。
\documentclass[margin=5pt]{standalone}
\usepackage{tikz,pgfplots,pgfplotstable}
\pgfplotstableread[col sep=comma, header=true]{
parameter, 50, 200
Material cost, 3.67, 8.69
E\&U cost, 5.13, 5.78
FCOP, 4.86, 6.31
ACC, 5.04, 5.94
}\loadedtable
\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlarge x limits=0.2,
%xmin=0,
xmax=10,
%xticklabels={0,2,4,6,8,10},
xbar,nodes near coords,bar width=16pt,bar shift=0pt,
symbolic y coords={Material cost, E\&U cost, FCOP, ACC },
ytick={Material cost, E\&U cost, FCOP, ACC },
y dir = reverse,
]
\addplot[nodes near coords align={left},
draw=blue,fill=blue!25,text=blue]
table[y=parameter,x expr={-\thisrow{50}},point meta={-x}] \loadedtable;
\addplot table[y=parameter,x expr={\thisrow{200}}] \loadedtable;
\end{axis}
\end{tikzpicture}
\end{document}
答案1
变体这个很好的解决方案就是要改变标签X 轴为 5.43。请注意,您不需要加载tikz
,pgfplots
如果您加载它们,则无论如何都会加载,但是如果您使用,则pgfplotstable
应该添加:\pgfplotsset{compat=1.18}
pgfplots
\documentclass[border=10pt]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\pgfplotstableread[col sep=comma, header=true]{
parameter, 50, 200
Material cost, 3.67, 8.69
E\&U cost, 5.13, 5.78
FCOP, 4.86, 6.31
ACC, 5.04, 5.94
}\loadedtable
\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlarge x limits=0.2,
%xmin=0,
xmax=10,
%xticklabels={0,2,4,6,8,10},
xbar,
nodes near coords,
bar width=16pt,
bar shift=0pt,
symbolic y coords={Material cost, E\&U cost, FCOP, ACC},
ytick={Material cost, E\&U cost, FCOP, ACC},
y dir=reverse,
xticklabel={\pgfmathparse{\tick+5.43}\pgfmathprintnumber{\pgfmathresult}}]
]
\addplot[nodes near coords align={left},
draw=blue, fill=blue!25, text=blue]
table[y=parameter, x expr={-\thisrow{50}}, point meta={-x}] \loadedtable;
\addplot table[y=parameter, x expr={\thisrow{200}}] \loadedtable;
\end{axis}
\end{tikzpicture}
\end{document}
也许这就是你想要的:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\pgfplotstableread[col sep=comma, header=true]{
parameter, 50, 200
Material cost, 3.67, 8.69
E\&U cost, 5.13, 5.78
FCOP, 4.86, 6.31
ACC, 5.04, 5.94
}\loadedtable
\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlarge x limits=0.2,
%xmin=0,
%xmax=2,
%xticklabels={0,2,4,6,8,10},
xbar,
nodes near coords,
bar width=16pt,
bar shift=0pt,
symbolic y coords={Material cost, E\&U cost, FCOP, ACC},
ytick={Material cost, E\&U cost, FCOP, ACC},
y dir=reverse,
xticklabel={\pgfmathparse{\tick+5.43}\pgfmathprintnumber{\pgfmathresult}}]
]
\addplot[nodes near coords align={left},
draw=blue, fill=blue!25, text=blue]
table[y=parameter, x expr={\thisrow{50}-5.43}, point meta={-x}] \loadedtable;
\addplot table[nodes near coords align={right},
y=parameter, x expr={\thisrow{200}-5.43}, point meta={x}] \loadedtable;
\end{axis}
\end{tikzpicture}
\end{document}
答案2
谢谢 - 后者是我想要的。在这种情况下,我可以将值显示为 %,并且不需要 x 轴偏移。
\documentclass[margin=5pt]{standalone}
\usepackage[dvipsnames,table,xcdraw]{xcolor}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=comma, header=true]{
parameter, 50, 200
Material cost, -31, 63
E\&U cost, -4, 8
FCOP, -9, 18
ACC, -6, 11
}\loadedtable
\begin{axis}[
enlarge x limits=0.2,
xlabel = {TCOP deviation},
xticklabel={\pgfmathprintnumber\tick\%},
tick label style={font=\footnotesize},
xlabel style={font=\footnotesize},
ylabel style={font=\footnotesize},
%enlarge y limits={0.1},
xmin=-50,
xmax=100,
%xticklabels={0,2,4,6,8,10},
xbar,
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%},
every node near coord/.append style={font=\scriptsize},
bar width=14pt,bar shift=0pt,
symbolic y coords={Material cost, E\&U cost, FCOP, ACC },
ytick={Material cost, E\&U cost, FCOP, ACC },
y dir = reverse,
legend pos=south east,
legend style={
font=\footnotesize}
]
\addplot[nodes near coords align={left},
draw=CornflowerBlue,fill=CornflowerBlue,text=CornflowerBlue]
table[y=parameter,x expr={\thisrow{50}}] \loadedtable;
\addplot[
draw=orange,fill=orange,text=orange]
table[y=parameter,x expr={\thisrow{200}}] \loadedtable;
\legend{-50\%, +100\%}
\end{axis}
\end{tikzpicture}
\end{document}