# Scaling the axes in pgfplots to plot the square root function

What is wrong with this code. I use unit vector ratio={2 1} to get a better visualization of the square root function and cube root function.

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc,positioning,intersections}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\noindent \hspace*{\fill}
\begin{tikzpicture}
\begin{axis}[height=4.5in,width=4.5in, clip=false,
unit vector ratio={2 1},
xmin=-100,xmax=100,
ymin=-5,ymax=10,
restrict y to domain=-5:10,
xtick={\empty},ytick={\empty},
enlargelimits={abs=1cm},
axis lines=middle,
axis line style={latex-latex},
xlabel=$x$,ylabel=$y$,
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]

\addplot[samples=501, domain=0:100, blue] {x^(1/2)} node[anchor=north west, pos=0.75, font=\footnotesize]{$y = \sqrt{x}$};
\addplot[samples=501, domain=-100:0, green] {-(-x)^(1/3)}
node[anchor=south east, pos=0.25, font=\footnotesize]{$y = \sqrt[\uproot{1} \leftroot{-1} n]{x}$};
\addplot[samples=501, domain=0:100, green] {x^(1/3)};

\end{axis}
\end{tikzpicture}

\end{document}


## 2 Answers

With unit vector ratio={2 1} the unit vector for the x-direction is twice as long as the unit vector in y-direction. But there are only 15 units in y-direction in your plot while there are 200 in x-direction. So if the y-axis should be 1cm long the x-axis must be 1cm*(200/15)*2=26.7cm long!

I would suggest to use something like unit vector ration={1 4} resulting in

Code:

\documentclass{amsart}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\noindent \hspace*{\fill}
\begin{tikzpicture}
\begin{axis}[height=4.5in,width=4.5in, clip=false,
unit vector ratio={1 4},
xmin=-100,xmax=100,
ymin=-5,ymax=10,
restrict y to domain=-5:10,
xtick={\empty},ytick={\empty},
enlargelimits={abs=1cm},
axis lines=middle,
axis line style={latex-latex},
xlabel=$x$,ylabel=$y$,
xlabel style={at={(ticklabel* cs:1)},anchor=north west},
ylabel style={at={(ticklabel* cs:1)},anchor=south west}
]
\addplot[samples=501, domain=0:100, blue] {x^(1/2)} node[anchor=north west, pos=0.75, font=\footnotesize]{$y = \sqrt{x}$};
\addplot[samples=501, domain=-100:0, green] {-(-x)^(1/3)}
node[anchor=south east, pos=0.25, font=\footnotesize]{$y = \sqrt[\uproot{1} \leftroot{-1} n]{x}$};
\addplot[samples=501, domain=0:100, green] {x^(1/3)};
\end{axis}
\end{tikzpicture}
\end{document}


Because of the question in a comment:

You set width=4.5in and height=4.5in. If you do not set unit vector ratio, ymin and ymax you get a square

\begin{axis}[height=4.5in,width=4.5in, clip=false,
%unit vector ratio={1 4},
xmin=-100,xmax=100,
%ymin=-5,ymax=10,
%restrict y to domain=-5:10,
%xtick={\empty},ytick={\empty},
...
]


With unit vector ratio={1 4} the scaling of the y-axis changes but you have still a square

\begin{axis}[height=4.5in,width=4.5in, clip=false,
unit vector ratio={1 4},
xmin=-100,xmax=100,
%ymin=-5,ymax=10,
%restrict y to domain=-5:10,
%xtick={\empty},ytick={\empty},
...
]


But then you restrict the displayed y-range using ymin and ymax and so the hight of the y-axis is decreased.

\begin{axis}[height=4.5in,width=4.5in, clip=false,
%unit vector ratio={1 4},
xmin=-100,xmax=100,
ymin=-5,ymax=10,
%restrict y to domain=-5:10,
%xtick={\empty},ytick={\empty},
...
]

• Yes, this is about the display that I want. I think that I will extend the y-axis upwards a bit more, but the graphs of the functions themselves are exactly what I want. Thanks. – Adelyn Feb 23 '16 at 20:15
• What do the values of 4.5in in the axis environment do for the display? I have height=4.5in and width=4.5in, but the display is not a square. (I do not want it to be a square.) – Adelyn Feb 23 '16 at 20:15
• @Adelyn See my updated answer. – esdd Feb 23 '16 at 21:05
• Thanks for the explanation and the graph illustrating it. So, restrict y to domain=-5:10 restricts the y-axis to the interval [-5, 10]. What would ymin=-5 and ymax=10 do for the graph? Would that plot only those points on the graph for which the y-value is between -5 and 10? Without restrict y to domain=-5:10, would the y-axis go between -25 and 25 if xmin=-100 and xmax=100? – Adelyn Feb 24 '16 at 21:05
• ymin and ymax restricts the shown range of the y-axis. If you remove ymin=-5 and ymax=10 from your code the y axis would go from -25 to 25 with your settings of width, height, xmin, xmax and unit vector ratio={1 4}. Note that restrict y to domain=-5:10 restricts only the shown plot but not the y-axis. And it has no effect in your example becaus all y values of both plots are largen than -5 and lower or equal than 10 if xmin=-100 and xmax=100. – esdd Feb 24 '16 at 22:43

The given code gives the expected result. The main problem is that you scaled the wrong parameter, so you squeezed the wrong axis.

Also you provide width, height and all axis limits (i.e. xmin, xmax, ymin, and ymax), so the question is, what has the higher priority to execute or it depends on the given order of the keys.

Here is a reduced code showing that everything works as expected. Hopefully this helps to modify your code to your needs, but I cannot help any more here, because your question is quite "vague".

\documentclass[border=2mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
% define a scaling factor for unit vector ratio'
\pgfmathsetmacro{\factor}{10}
% define a lenght to draw in y direction for testing,
% if unit vector ratio' is working as expected
\pgfmathsetmacro{\Ydirection}{5}
\begin{axis}[
clip=false,
unit vector ratio={1 \factor},
restrict y to domain=-5:10,
xlabel=$x$,ylabel=$y$,
]
\addplot[samples=51, domain=0:100, blue] {x^(1/2)}
node[anchor=north west, pos=0.75, font=\footnotesize]
{$y = \sqrt{x}$};
\addplot[samples=51, domain=0:100, green] {x^(1/3)};

% draw some lines for testing, if the unit vector ratio' is
% working as expected and save the beginning and ending coordinates
\draw [red] (0,0) -- +(axis direction cs: \factor*\Ydirection,0)
coordinate [pos=0] (origin)
coordinate [pos=1] (x)
;
\draw [red] (0,0) -- +(axis direction cs: 0,\Ydirection)
coordinate [pos=1] (y)
;
\end{axis}
\path   let
% calculate "dummy" coordinates giving the coordinates
% of the difference between the points
% (because the one is at the origin it should give
%  the same values as the first coordinate)
\p1 = ($(x) - (origin)$),
\p2 = ($(y) - (origin)$),
% calculate the vector lengths of the "dummy points"
\n1 = {veclen(\x1,\y1)},
\n2 = {veclen(\x2,\y2)}
in
% plot the calculated length of the vectors, which should
% be identical (if there are no rounding errors)
node [anchor=west]  at (x) {\n1}
node [anchor=south] at (y) {\n2}
;
\end{tikzpicture}
\end{document}
`