我想了解是否可以让 x 轴自动以 0 为中心,即让x min
和的绝对轴选项值x max
相等,而无需明确设置它们。
以下 MWE 显示了这个问题。我有点估计值及其置信区间。最大值为 -3.5 和 3.5,但是,将它们设置为x min
或x max
会降低可读性(缩小图)。另一方面,如果零点居中,比较点估计值会更容易。
梅威瑟:
\documentclass{article}
\usepackage{pgfplots}
\begin{filecontents}{data.csv}
nr est CL CU
1 -0.9 -1.3 -0.3
2 0 -0.6 0.58
\end{filecontents}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmajorgrids]
\addplot+[only marks,error bars/.cd,x explicit,x dir=both] table [
x=est,
y = nr,
x error plus expr=\thisrow{CU}-\thisrow{est},
x error minus expr=\thisrow{est}-\thisrow{CL}]{data.csv};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
我不确定是否有自动解决方案,因为 不会设置\pgfkeysvalueof{/pgfplots/xmin}
和,但使用\pgfkeysvalueof{/pgfplots/xmax}
很容易推断xmin
和的实际值。它们可以在最终的图中使用。xmax
calc
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
nr est CL CU
1 -0.9 -1.3 -0.3
2 0 -0.6 0.58
\end{filecontents*}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.16}
\begin{document}
Step 1: measure the $x_\mathrm{min}$ and $x_\mathrm{max}$ used in the plot.
\begin{tikzpicture}
\begin{axis}[xmajorgrids]
\addplot+[only marks,error bars/.cd,x explicit,x dir=both] table [
x=est,
y = nr,
x error plus expr=\thisrow{CU}-\thisrow{est},
x error minus expr=\thisrow{est}-\thisrow{CL}]{data.csv};
\path (0,0) coordinate (O) (1,0) coordinate(X);
\end{axis}
\path let \p1=($(current axis.west)-(O)$),\p2=($(current axis.east)-(O)$),
\p3=($(X)-(O)$) in
\pgfextra{\pgfmathsetmacro{\myxmin}{\x1/\x3}%
\pgfmathsetmacro{\myxmax}{\x2/\x3}%
\message{xmin=\myxmin, xmax=\myxmax^^J}%
\pgfmathsetmacro{\newxmin}{-1*max(abs(\myxmin),abs(\myxmax))}%
\xdef\newxmin{\newxmin}%
\pgfmathsetmacro{\newxmax}{max(abs(\myxmin),abs(\myxmax))}%
\xdef\newxmax{\newxmax}};
\end{tikzpicture}
Step 2: measure the $x_\mathrm{min}$ and $x_\mathrm{max}$ used in the plot.
\begin{tikzpicture}
\begin{axis}[xmajorgrids,xmin=-1.488,xmax=1.488]
\addplot+[only marks,error bars/.cd,x explicit,x dir=both] table [
x=est,
y = nr,
x error plus expr=\thisrow{CU}-\thisrow{est},
x error minus expr=\thisrow{est}-\thisrow{CL}]{data.csv};
\end{axis}
\end{tikzpicture}
\clearpage
You can also use the computed values.
\begin{tikzpicture}
\begin{axis}[xmajorgrids,xmin=\newxmin,xmax=\newxmax]
\addplot+[only marks,error bars/.cd,x explicit,x dir=both] table [
x=est,
y = nr,
x error plus expr=\thisrow{CU}-\thisrow{est},
x error minus expr=\thisrow{est}-\thisrow{CL}]{data.csv};
\end{axis}
\end{tikzpicture}
\end{document}
您可以通过将推断值写入辅助文件来实现完全自动化。如果需要,我很乐意详细说明。 附录:这是一个可以实现该功能的版本。
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{data.csv}
nr est CL CU
1 -0.9 -1.3 -0.3
2 0 -0.6 0.58
\end{filecontents*}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.16}
\makeatletter
\newcommand{\ExportXmaxmin}{\immediate\write\@mainaux{\xdef\string\newxmin{\newxmin}\relax%
\xdef\string\newxmax{\newxmax}\relax}}
\makeatother
\begin{document}
\begin{tikzpicture}
\ifdefined\newxmin
\pgfplotsset{adjusted axis/.style={xmin=\newxmin,xmax=\newxmax}}
\message{Read xmin=\newxmin\space and xmin=\newxmax\space from aux file.^^J}
\else
\pgfplotsset{adjusted axis/.style={}}
\message{Compile the document again to get the adjusted xmin and xmax values.^^J}
\fi
\begin{axis}[xmajorgrids,adjusted axis]
\addplot+[only marks,error bars/.cd,x explicit,x dir=both] table [
x=est,
y = nr,
x error plus expr=\thisrow{CU}-\thisrow{est},
x error minus expr=\thisrow{est}-\thisrow{CL}]{data.csv};
\path (0,0) coordinate (O) (1,0) coordinate(X);
\end{axis}
\ifdefined\newxmin
\else
\path let \p1=($(current axis.west)-(O)$),\p2=($(current axis.east)-(O)$),
\p3=($(X)-(O)$) in
\pgfextra{\pgfmathsetmacro{\myxmin}{\x1/\x3}%
\pgfmathsetmacro{\myxmax}{\x2/\x3}%
\pgfmathsetmacro{\newxmin}{-1*max(abs(\myxmin),abs(\myxmax))}%
\pgfmathsetmacro{\newxmax}{max(abs(\myxmin),abs(\myxmax))}%
\ExportXmaxmin};
\fi
\end{tikzpicture}
\end{document}