pgfplots:如何以稳健的方式将原点添加到轴?

pgfplots:如何以稳健的方式将原点添加到轴?

问题

我将原点添加到我的图表中

after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}

(找到代码)这里)。如果原点在我的轴上,则此方法可行。但是,如果0不在所取的值中,则会出现问题y(见下图)。

有办法解决这个问题吗?

我的意思是,只有在需要时才显示原点的选项。

图片

第一张照片

在此处输入图片描述

第二张图片

我无法把它放在这里,但它打印了两页!!! 在此处输入图片描述

代码

第一个代码

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[grid = major, 
clip = true, 
clip mode=individual, 
axis x line = middle, 
axis y line = middle, 
xlabel={$x$}, 
xlabel style={at=(current axis.right of origin), anchor=west}, 
ylabel={$y$}, 
ylabel style={at=(current axis.above origin), anchor=south}, 
domain = 2:5, 
xmin = 2, 
xmax = 5, 
enlarge y limits={rel=0.13}, 
enlarge x limits={rel=0.07}, 
restrict y to domain=1:2,
ymin = 1, 
ymax = 2, 
after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] gnuplot{atan(x)-1/2};

\end{axis}
\end{tikzpicture}

\end{document}

第二个代码

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[grid = major, 
clip = true, 
clip mode=individual, 
axis x line = middle, 
axis y line = middle, 
xlabel={$x$}, 
xlabel style={at=(current axis.right of origin), anchor=west}, 
ylabel={$y$}, 
ylabel style={at=(current axis.above origin), anchor=south}, 
domain = 2:5, 
xmin = 2, 
xmax = 5, 
enlarge y limits={rel=0.13}, 
enlarge x limits={rel=0.07}, 
%restrict y to domain=1:2,
%ymin = 1, 
%ymax = 2, 
after end axis/.code={\path (axis cs:0,0) node [anchor=north west,yshift=-0.075cm] {0} node [anchor=south east,xshift=-0.075cm] {0};}]

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] gnuplot{atan(x)-1/2};

\end{axis}
\end{tikzpicture}

\end{document}

答案1

您可以axis direction cs使用

\path (axis direction cs:0,0) node [anchor=north west,yshift=-0.65cm,xshift=-0.7cm] {0} node [anchor=south east,xshift=-0.5cm,yshift=-0.7cm] {0};}]

代码:

\documentclass{article}
\usepackage[x11names]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[grid = major,
clip = true,
clip mode=individual,
axis x line = middle,
axis y line = middle,
xlabel={$x$},
xlabel style={at=(current axis.right of origin), anchor=west},
ylabel={$y$},
ylabel style={at=(current axis.above origin), anchor=south},
domain = 2:5,
xmin = 2,
xmax = 5,
enlarge y limits={rel=0.13},
enlarge x limits={rel=0.07},
restrict y to domain=1:2,
ymin = 1,
ymax = 2,
after end axis/.code={\path (axis direction cs:0,0) node [anchor=north west,yshift=-0.65cm,xshift=-0.7cm] {0} node [anchor=south east,xshift=-0.5cm,yshift=-0.7cm] {0};}]

\addplot[color=Gold3, samples=1000, smooth, ultra thick, unbounded coords=jump, no markers] gnuplot{atan(x)-1/2};

\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

您可能axis description cs还想利用它来实现此目的。

after end axis/.code={\path (axis description cs:0,0) node [anchor=north west,yshift=-0.075cm,xshift=-0.2cm] {0} node [anchor=south east,xshift=-0.075cm,yshift=-0.2cm] {0};}]

然而,对于这个特定的图表,(0,0)位置完全错误,如图所示。

相关内容