所以我一直在尝试为我的经济学课程绘制一个函数图。我的问题是创建的“轴”与实际点不匹配;例如,点 (0,5) 不会在 y 轴上,它会移位。
以下是我的图表的代码:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}[scale=1.5,line width=1pt]
\begin{axis}[
color= black,
xmin=0,
xmax=19.5,
ymin=0,
ymax=19.5,
axis equal,
axis x line=left,
axis y line=left,
disabledatascaling,
xticklabels={},
yticklabels={},
font=\scriptsize,
ticks=none,
extra x ticks=0,
extra y ticks=0,
]
\draw (0,5) -- (10,0);
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
我的问题不在于创建图表;我只是画了一条简单的线来表明点没有对齐。
有人知道为什么会发生这种情况吗?我完全搞不懂。非常感谢您的帮助!
答案1
正如 David 指出的那样他的回答这是因为该axis equal
选项会改变轴限制以适应指定的width
和。height
axis
另一种选择是使用axis equal image
键,它可以改变width
或height
。
如果你总是想要确保轴线绘制在零坐标处,你应该使用axis lines=middle
而不是axis lines=left
,正如 Henri Menke 在在问题下方评论。
(我还在我的答案代码中对您的代码做了一些其他评论,您应该看看。以防您不知道这些内容。)
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% % (use this `compat' level or higher so by default TikZ coordinates use
% % `axis cs:'. See comments below for more details.)
% \pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,
xmax=19.5,
ymin=0,
ymax=19.5,
% ---------------------------------------------------------------------
% replace `axis equal' with `axis equal image'
% axis equal,
axis equal image,
% ---------------------------------------------------------------------
% % (if you want to be sure that the axis lines are drawn at the
% % zero coordinates, than you should use `axis lines=middle'.
% % Please note that then by default the zero ticklabels are not
% % drawn, because in general these would be written on "the other"
% % axis line. To make them appear nonetheless you could add
% % `hide obscured x ticks=false' (and similar for the y axis), as
% % are commented below the next commented option line.)
axis lines=left,
% axis lines=middle,
% hide obscured x ticks=false,
% hide obscured y ticks=false,
% % (if you comment `disabledatascaling' you also don't get the desired
% % result, because with the default `compat' level TikZ coordinates
% % by default don't use the axis coordinate system (`axis cs:'), but
% % the tikzpicture coordinate system (`cs:'). ...
% disabledatascaling,
]
% (... To overcome this issue you could either prepend all TikZ
% coordinates by `axis cs:' or use a `compat' value of 1.11 or higher)
\draw [red,thick] ( 0,5) -- ( 10,0);
\draw [green] (axis cs:0,5) -- (axis cs:10,0);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
问题出在这条axis equal
线上。axis equal
它会自动更改轴限值,以使绘图适合指定的高度和宽度。您可以通过注释掉这条ticks=none
线来看到它正在更改 x 轴 - 0 刻度标记不在 y 轴上。
我建议删除该axis equal
线。