我有以下 tikzpicture,其中轴标签固定在轴的末端。(代码在底部)
到目前为止一切都很好,除了当我想缩放轴时(取消注释下面的行),标签会偏离假定的位置。在此示例中,即使 x 和 y 比例设置为相同值,它也会失败。(如果我scale=0.7
在这种情况下这样做,它会起作用,但一般来说,两个轴可能还需要不同的比例因子)
请问有什么建议可以修复此问题吗?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.17,
mystyle/.style={
axis x line=middle,
axis y line=middle,
every axis x label/.style={at={(current axis.right of origin)},anchor=west},
every axis y label/.style={at={(current axis.above origin)},anchor=south},
every axis plot/.append style={mark = none, draw=black},
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
mystyle,
xlabel=xlabel,
ylabel=ylabel,
% xscale=0.7, yscale=0.7 % uncomment this
]
\addplot coordinates {(1,1) (2,-1)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
如果你更换
every axis x label/.style={at={(current axis.right of origin)},anchor=west}
和
xlabel style={anchor=west},
然后使用xscale=0.7
就可以了:
笔记:
轴标签被修改为数学模式。
使用
ymax=1.1
消除了点位于箭头上方的问题。
代码:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.17,
mystyle/.style={
axis x line=middle,
axis y line=middle,
%every axis x label/.style={at={(current axis.right of origin)},anchor=west},
xlabel style={anchor=west},% <----- Revision
every axis y label/.style={at={(current axis.above origin)},anchor=south},
every axis plot/.append style={mark = none, draw=black},
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
mystyle,
xlabel=$x$,% <----- Now in math mode
ylabel=$y$,
xscale=0.7, yscale=0.7,
ymax=1.1,% <----- Added
]
\addplot coordinates {(1,1) (2,-1)};
\end{axis}
\end{tikzpicture}
\end{document}