当我使用 Tikz 绘制 $y=x+\frac{1}{x}$ 和 $y=5(x-\frac{4}{3})$ 的图形时,发现 $x$ 轴的单位长度与 $y$ 轴的单位长度不一样,如果我想让它们相同,该怎么办?
答案1
您可以通过说 或 来强制执行相等的单位unit vector ratio=1 1 1
。axis equal image
差异axis equal
很微妙。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{pdflscape}
\begin{document}
\begin{landscape}
\begin{tikzpicture}[
declare function={
f(\x) = \x+1/\x;
g(\x) = 5*(\x-4/3);}]
\begin{axis}[unit vector ratio=1 1 1,
axis x line=middle, axis y line=middle,
ymin=-5, ymax=5,ylabel=$y$,
xmin=0,xmax=5.5,xlabel=$x$,
domain=0.1:5,samples=101,
legend pos=south east]
\addplot [blue,thick] {f(x)};
\addlegendentry{$f(x)$}
\addplot [red,thick] {g(x)};
\addlegendentry{$g(x)$}
\end{axis}
\node[anchor=north] at (current axis.south) {\texttt{unit vector ratio=1 1 1}};
\end{tikzpicture}
~
\begin{tikzpicture}[
declare function={
f(\x) = \x+1/\x;
g(\x) = 5*(\x-4/3);}]
\begin{axis}[axis equal image,
axis x line=middle, axis y line=middle,
ymin=-5, ymax=5,ylabel=$y$,
xmin=0,xmax=5.5,xlabel=$x$,
domain=0.1:5,samples=101,
legend pos=south east]
\addplot [blue,thick] {f(x)};
\addlegendentry{$f(x)$}
\addplot [red,thick] {g(x)};
\addlegendentry{$g(x)$}
\end{axis}
\node[anchor=north] at (current axis.south) {\texttt{axis equal image}};
\end{tikzpicture}
~
\begin{tikzpicture}[
declare function={
f(\x) = \x+1/\x;
g(\x) = 5*(\x-4/3);}]
\begin{axis}[axis equal,
axis x line=middle, axis y line=middle,
ymin=-5, ymax=5,ylabel=$y$,
xmin=0,xmax=5.5,xlabel=$x$,
domain=0.1:5,samples=101,
legend pos=south east]
\addplot [blue,thick] {f(x)};
\addlegendentry{$f(x)$}
\addplot [red,thick] {g(x)};
\addlegendentry{$g(x)$}
\end{axis}
\node[anchor=north] at (current axis.south) {\texttt{axis equal}};
\end{tikzpicture}
\end{landscape}
\end{document}