3

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?

6
  • Welcome to TeX.SE! Are you looking for unit vector ratio=1 1 1? – user121799 Nov 9 '18 at 21:46
  • 1
    Unrelated note: TikZ and pgfplots are two separate packages. The latter uses the former, but one can make plots in TikZ without using pgfplots. That unit vector ratio worked means that you use pgfplots I guess, so it's better to say that. (By the the way, axis equal is a shorthand for unit vector ratio=1 1 1.) – Torbjørn T. Nov 9 '18 at 23:24
  • @TorbjørnT. wow great advice! – manooooh Nov 9 '18 at 23:26
  • 1
    @TorbjørnT. The pgfplots manual says on p. 299: "The configuration axis equal image=true is actually just a style which sets unit 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
  • 2
    @marmot Oops, you're right. I must have misread, thanks for the correction. – Torbjørn T. Nov 10 '18 at 0:01
5

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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.