pgfplots - 当使用符号 x 坐标时,如何为 y 绘制零线(从 xmin 到 xmax)?

pgfplots - 当使用符号 x 坐标时,如何为 y 绘制零线(从 xmin 到 xmax)?

根据 Jake 提供的代码

\draw[very thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);

我正在为条形图的 y 值绘制一条零线。但如果使用 ,则不起作用symbolic x coords=...

也可以写成\draw[very thin] (axis cs:Fx,0) -- (axis cs:Fz,0);,但这还不够。使用Fx.westFx.left不起作用。

平均能量损失

\documentclass[
a4paper
]{scrartcl}

\usepackage{
amsmath,
tikz,
pgfplots,
pgfplotstable
}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\begin{filecontents}{force-stats.txt}
Stats,Min,MinStdDev,MinCIP95,Max,MaxStdDev,MaxCIP95
Fx,-70.35,0.5,0.6,60,1,0.5
Fy,-40,0.5,0.6,50,1.91,1.5
Fz,-20,0.42,0.6,40,4,0.5
\end{filecontents}

\pgfplotstableread[col sep=comma]{force-stats.txt}{\tableabcdef}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{axis}[
scale only axis=true,
width=5cm,
height=7cm,
%
xlabel={Force components},
ylabel={Force in N},
%
ybar,
%
xmajorgrids=true,
ymajorgrids=true,
%
enlarge x limits={0.3},
enlarge y limits={0.2},
%
symbolic x coords={Fx,Fy,Fz},
%
xtick=data,
nodes near coords,
xticklabels={{\(F_{x}\)},{\(F_{y}\)},{\(F_{z}\)}},
]
%the following line will not work
%\draw[very thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot+[nodes near coords align={above}] plot [error bars/.cd, y dir=both, y explicit] table [col sep=comma, x=Stats, y=Max, y error=MinCIP95] {\tableabcdef};
\addplot+[nodes near coords align={below}] plot [error bars/.cd, y dir=both, y explicit] table [col sep=comma, x=Stats, y=Min, y error=MinCIP95] {\tableabcdef};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

答案1

您可以使用

after end axis/.append code={
  \draw[very thick] (current axis.left of origin) -- (current axis.right of origin);
}

在此处输入图片描述

代码:

\documentclass{scrartcl}

\usepackage{
amsmath,
pgfplots,
pgfplotstable
}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\begin{filecontents}{force-stats.txt}
Stats,Min,MinStdDev,MinCIP95,Max,MaxStdDev,MaxCIP95
Fx,-70.35,0.5,0.6,60,1,0.5
Fy,-40,0.5,0.6,50,1.91,1.5
Fz,-20,0.42,0.6,40,4,0.5
\end{filecontents}

\pgfplotstableread[col sep=comma]{force-stats.txt}{\tableabcdef}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
\begin{axis}[
scale only axis=true,
width=5cm,
height=7cm,
%
xlabel={Force components},
ylabel={Force in N},
%
ybar,
%
xmajorgrids=true,
ymajorgrids=true,
%
enlarge x limits={0.3},
enlarge y limits={0.2},
%
symbolic x coords={Fx,Fy,Fz},
%
xtick=data,
nodes near coords,
xticklabels={{\(F_{x}\)},{\(F_{y}\)},{\(F_{z}\)}},
after end axis/.append code={
  \draw[very thick] (current axis.left of origin) -- (current axis.right of origin);
}
]
\addplot+[nodes near coords align={above}] plot [error bars/.cd, y dir=both, y explicit] table [col sep=comma, x=Stats, y=Max, y error=MinCIP95] {\tableabcdef};
\addplot+[nodes near coords align={below}] plot [error bars/.cd, y dir=both, y explicit] table [col sep=comma, x=Stats, y=Min, y error=MinCIP95] {\tableabcdef};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}

答案2

根据第二条建议percusse 对相关问题的回答,对于这种情况,您可以使用相对轴坐标。

例如,此代码将在 y=20 处产生一条红色水平线:

\draw[very thin,red] 
       ({rel axis cs:0,0} |- {axis cs:Fx,-20}) 
    -- ({axis cs:Fz,-20} -| {rel axis cs:1,0});

您可能希望将其制作成宏,因为它需要输入两次所需的 y 坐标。

在此处输入图片描述

\documentclass[
a4paper
]{scrartcl}

\usepackage{
  amsmath,
  tikz,
  pgfplots,
  pgfplotstable
}

\usetikzlibrary{calc}

\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\begin{filecontents}{force-stats.txt}
  Stats,Min,MinStdDev,MinCIP95,Max,MaxStdDev,MaxCIP95
  Fx,-70.35,0.5,0.6,60,1,0.5
  Fy,-40,0.5,0.6,50,1.91,1.5
  Fz,-20,0.42,0.6,40,4,0.5
\end{filecontents}

\pgfplotstableread[col sep=comma]{force-stats.txt}{\tableabcdef}

\begin{document}
\begin{center}
  \begin{tikzpicture}[font=\small]
    \begin{axis}[
      scale only axis=true,
      width=5cm,
      height=7cm,
      %
      xlabel={Force components},
      ylabel={Force in N},
      %
      ybar,
      %
      xmajorgrids=true,
      ymajorgrids=true,
      %
      enlarge x limits={0.3},
      enlarge y limits={0.2},
      %
      symbolic x coords={Fx,Fy,Fz},
      %
      xtick=data,
      nodes near coords,
      xticklabels={{\(F_{x}\)},{\(F_{y}\)},{\(F_{z}\)}},
      ]
      %the following line will not work
      %\draw[very thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
      \addplot+[nodes near coords align={above}] plot [error bars/.cd, y dir=both, y explicit] table [col sep=comma, x=Stats, y=Max, y error=MinCIP95] {\tableabcdef};
      \addplot+[nodes near coords align={below}] plot [error bars/.cd,
      y dir=both, y explicit] table [col sep=comma, x=Stats, y=Min, y
      error=MinCIP95] {\tableabcdef};
      %% NEW
      \draw[very thin,green] ({rel axis cs:0,0} |- {axis cs:Fx,0}) --
      ({axis cs:Fz,0} -| {rel axis cs:1,0});
      \draw[very thin,blue] ({rel axis cs:0,0} |- {axis cs:Fx,40}) --
      ({axis cs:Fz,40} -| {rel axis cs:1,0});
      \draw[very thin,red] ({rel axis cs:0,0} |- {axis cs:Fx,-20}) --
      ({axis cs:Fz,-20} -| {rel axis cs:1,0});
      %% END NEW
    \end{axis}
  \end{tikzpicture}
\end{center}
\end{document}

相关内容