对于任何常见的 x>0,y>0 函数,pgfplot 中的标准轴箭头位置都很好
不幸的是,对于更多的负函数(域或范围),箭头有点奇怪(红色=我想要的)。
似乎可以手动设置箭头,如下所示pgfplots:y 轴箭头朝下
然而,如果能够让情节使用正确的箭头,那就太棒了自动!
我正在思考某种逻辑,例如:
- 如果 x 轴 >0,使用右箭头
- 如果 x 轴 <0,使用左箭头
- 如果 y 轴 >0,则使用顶部箭头
- 如果 y 轴小于 0,则使用底部箭头
但是我不知道该如何做,有人愿意帮忙吗?
平均能量损失
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{arrows.meta}
\pgfplotsset{
no marks,axis lines=middle,
inner axis line style={-{Latex[length=4mm]}}
}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[domain=0:1]
\addplot+{x};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
答案1
我认为,这符合您的建议。iaxis
样式会查看原点是位于轴边界的左侧/右侧/上方/下方,并相应地添加箭头。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows.meta,calc}
\pgfplotsset{no marks,
iaxis/.style={,axis lines=middle,
inner axis line style={-},clip mode=individual,execute at end axis={
\path[tips=proper] let \p1=($(rel axis cs:1,1)-(axis cs:0,0)$),
\p2=($(axis cs:0,0)-(rel axis cs:0,0)$)
in %\pgfextra{\typeout{\x1,\x2,\y1,\y2}}
\ifdim\x1>0pt
(axis cs:0,0) edge[-{Latex[length=4mm]}] ({rel axis cs:1,0} |- {axis cs:0,0})
\fi
\ifdim\x2>0pt
(axis cs:0,0) edge[-{Latex[length=4mm]}] ({rel axis cs:0,0} |- {axis cs:0,0})
\fi
\ifdim\y1>0pt
(axis cs:0,0) edge[-{Latex[length=4mm]}] ({rel axis cs:0,1} -| {axis cs:0,0})
\fi
\ifdim\y2>0pt
(axis cs:0,0) edge[-{Latex[length=4mm]}] ({rel axis cs:0,0} -| {axis cs:0,0})
\fi;
}},
every axis/.append style=iaxis
}
\begin{document}
\begin{tabular}{cc}
\begin{tikzpicture}
\begin{axis}[domain=0:1]
\addplot+{x};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}
\begin{axis}[domain=0:1]
\addplot+{-x};
\end{axis}
\end{tikzpicture}\\
\begin{tikzpicture}
\begin{axis}[domain=-1:0]
\addplot+{x};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}
\begin{axis}[domain=-1:0]
\addplot+{-x};
\end{axis}
\end{tikzpicture}\\
\begin{tikzpicture}
\begin{axis}[domain=-1:1]
\addplot+{-x};
\end{axis}
\end{tikzpicture}
\end{tabular}
\end{document}
或者使用超出范围的箭头(这会改变轴对象的尺寸)。
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{arrows.meta,calc}
\pgfplotsset{no marks,
iaxis/.style={axis lines=middle,
inner axis line style={-},clip mode=individual,execute at end axis={
\path[tips=proper] let \p1=($(rel axis cs:1,1)-(axis cs:0,0)$),
\p2=($(axis cs:0,0)-(rel axis cs:0,0)$)
in %\pgfextra{\typeout{\x1,\x2,\y1,\y2}}
\ifdim\x1>0pt
(axis cs:0,0) edge[-{Latex[length=4mm]}]
($({rel axis cs:1,0} |- {axis cs:0,0})+($(4mm,0)-(0,0)$)$)
\fi
\ifdim\x2>0pt
(axis cs:0,0) edge[-{Latex[length=4mm]}]
($({rel axis cs:0,0} |- {axis cs:0,0})+($(-4mm,0)-(0,0)$)$)
\fi
\ifdim\y1>0pt
(axis cs:0,0) edge[-{Latex[length=4mm]}]
($({rel axis cs:0,1} -| {axis cs:0,0})+($(0,4mm)-(0,0)$)$)
\fi
\ifdim\y2>0pt
(axis cs:0,0) edge[-{Latex[length=4mm]}]
($({rel axis cs:0,0} -| {axis cs:0,0})+($(0,-4mm)-(0,0)$)$)
\fi;
}},
every axis/.append style=iaxis
}
\begin{document}
\begin{tabular}{cc}
\begin{tikzpicture}
\begin{axis}[domain=0:1]
\addplot+{x};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}
\begin{axis}[domain=0:1]
\addplot+{-x};
\end{axis}
\end{tikzpicture}\\
\begin{tikzpicture}
\begin{axis}[domain=-1:0]
\addplot+{x};
\end{axis}
\end{tikzpicture} &
\begin{tikzpicture}
\begin{axis}[domain=-1:0]
\addplot+{-x};
\end{axis}
\end{tikzpicture}\\
\begin{tikzpicture}
\begin{axis}[domain=-1:1]
\addplot+{-x};
\end{axis}
\end{tikzpicture}
\end{tabular}
\end{document}