When I use Tikz to draw the graphs of $y=x+\frac{1}{x}$ and $y=5(x-\frac{4}{3})$, but I found the unit length of $x$ axis and that of $y$ axis are different, if I want to let them be the same, what should I do?
You can enforce equal units by saying unit vector ratio=1 1 1
, axis equal image
or axis equal
. The differences are subtle.
\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}
unit vector ratio=1 1 1
? – user121799 Nov 9 '18 at 21:46pgfplots
are two separate packages. The latter uses the former, but one can make plots in TikZ without usingpgfplots
. Thatunit vector ratio
worked means that you usepgfplots
I guess, so it's better to say that. (By the the way,axis equal
is a shorthand forunit vector ratio=1 1 1
.) – Torbjørn T. Nov 9 '18 at 23:24axis equal image=true
is actually just a style which setsunit vector ratio=1 1 1,unit rescale keep size=false
." so that I think that there is a slight difference (which however does not matter here). And yes, I guessed from the context that pgfplots was used since TikZ alone does not rescale units. – user121799 Nov 9 '18 at 23:55