pgfplots - 零线绘制在其他任何东西上,尤其是节点/引脚

pgfplots - 零线绘制在其他任何东西上,尤其是节点/引脚

为了向 pgfplots 生成的每个图添加零线,我使用了 percusse 提供的代码这里。(收到 2 条回复后,我简短地说明一下:这是一个“全局”解决方案。如果只想将零线添加到一个图,请参阅esdd 的回复

零线是添加到每个轴环境的最后一项,因此它们将绘制在之前绘制的任何内容之上。如果引脚或节点位于 y 轴旁边/上,则这有点难看。当然,我可以通过更改其参数来移动某些引脚的位置,但总而言之,有些引脚必须保持在 y 轴附近/上,因此并非所有引脚都可行。

这个问题能解决吗?或者有人能提供一些建议吗?

问题图片

在此处输入图片描述

平均能量损失

\documentclass{article}
\usepackage{pgfplots}

\tikzset{
every pin/.style={
    draw=black,
    fill=white
},
every pin edge/.style={
    draw=black!60,
    fill=white,
    thick
},}

\pgfplotsset{
every axis/.append style={
%
xmajorgrids={true},
ymajorgrids={true},
xminorgrids={false},
yminorgrids={false},
%
after end axis/.code={
    %   \fill[red] (axis cs:0,0) circle(5pt);
    \draw[thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
    \draw[thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
},
},
every tick/.append style={
    black,
    thick
},
/pgf/number format/.cd,
use comma,
1000 sep={.},
}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}
    \addplot {rand};
    \filldraw (axis cs:0.9,0.9) circle [radius=1pt];
    \node[pin={[pin distance=0.3cm]180:{0.9} }] at (axis cs:0.9,0.9) {};
    \node[pin=0:{0.9}] at (axis cs:0.9,0.9) {};
    \end{axis}
    \end{tikzpicture}
\end{document}

答案1

您可以通过按键将绘图作为第一个要绘制的内容移动execute at begin axis。因此,唯一需要修改的是

execute at begin axis={%
    %   \fill[red] (axis cs:0,0) circle(5pt);
    \draw[thin] (axis cs:\pgfkeysvalueof{/pgfplots/xmin},0) -- (axis cs:\pgfkeysvalueof{/pgfplots/xmax},0);
    \draw[thin] (axis cs:0,\pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:0,\pgfkeysvalueof{/pgfplots/ymax});
},

然后你得到,

在此处输入图片描述

答案2

current axis以下是使用带有锚点above originbelow originleft of origin的特殊节点的建议right of origin

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\tikzset{
  every pin/.style={draw=black,fill=white},
  every pin edge/.style={draw=black!60,fill=white,thick},
}

\pgfplotsset{
  every axis/.append style={
    xmajorgrids={true},
    ymajorgrids={true},
    xminorgrids={false},
    yminorgrids={false},
  },
  every tick/.append style={black,thick},
  /pgf/number format/.cd,
  use comma,
  1000 sep={.},
}

\begin{document}
\begin{tikzpicture}
  \begin{axis}
% zero lines:
    \draw[thin] (current axis.above origin) -- (current axis.below origin);
    \draw[thin] (current axis.left of origin) -- (current axis.right of origin);
%
    \addplot {rand};
    \filldraw (axis cs:0.9,0.9) circle [radius=1pt];
    \node[pin={[pin distance=0.3cm]180:{0.9} }] at (axis cs:0.9,0.9) {};
    \node[pin=0:{0.9}] at (axis cs:0.9,0.9) {};
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:当然这个节点也可以用于execute at begin axis

\pgfplotsset{
  every axis/.append style={
    xmajorgrids={true},
    ymajorgrids={true},
    xminorgrids={false},
    yminorgrids={false},clip=false,
  },
  execute at begin axis={%
    \draw[thin] (current axis.above origin) -- (current axis.below origin);
    \draw[thin] (current axis.left of origin) -- (current axis.right of origin);
  },
  every tick/.append style={black,thick},
  /pgf/number format/.cd,
  use comma,
  1000 sep={.},
}

相关内容