0

I want to extend the y-axis so that the value of the maximum on the graph isn't at tip of the y-axis arrow. (I also want to extend the x-axis for the same reason). See an example below: Picture What I want should look something like this (found in: https://tikz.dev/dv-axes) Another pic

I would also like to include ticks as in this picture to my example.

Thank you! (Sorry for big pictures, I do not know how to scale them here)

Codes:

\documentclass[11pt]{article}
\usepackage[a4paper,hmargin=1in,top=1.2in,bottom=1in]{geometry}

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

\begin{document}

\begin{tikzpicture} \begin{axis}[ xmin=-5, xmax=5, ymin=-0.5, ymax=5, ticks=none, axis y line=middle, axis x line=middle, axis line style=thick, ytick pos=upper, xlabel=$(1)$, ylabel=$(2)$, x label style={anchor=west}, y label style={anchor=south}, ] \addplot [ ultra thick, domain=-10:10, color=black!50!green, smooth ] {0.3333333*x^2}; %\addlegendentry{(\frac{1}{3}x^2)}

\addplot [ ultra thick, domain=-10:10, color=black!20!blue, smooth ] {x^2}; %\addlegendentry{(x^2)}

\addplot [ ultra thick, domain=-10:10, color=black!10!red, smooth ] {3*x^2}; %\addlegendentry{(3x^2)} \end{axis} \end{tikzpicture}

\end{document}

UnknownW
  • 399

2 Answers2

1

You can use restrict y to domain=0:5 and axis line style={-to}

\begin{tikzpicture}
\begin{axis}[
xmin=-5,    xmax=5,
ymin=-0.5,    ymax=6,
ticks=none,
axis y line=middle,
axis x line=middle,
axis line style={-to},
restrict y to domain=0:5,
axis line style=thick,
ytick pos=upper,
xlabel=$(1)$,   ylabel=$(2)$,
x label style={anchor=west},
y label style={anchor=south},
]
\addplot [
ultra thick,
domain=-5:5, 
color=black!50!green,
smooth,
samples=750,
]
{x^2/3};
%\addlegendentry{\(\frac{1}{3}x^2\)}

\addplot [ ultra thick, domain=-5:5, color=black!20!blue, smooth, samples=500, ] {x^2}; %\addlegendentry{(x^2)}

\addplot [ ultra thick, domain=-5:5, color=black!10!red, smooth, samples=1100, ] {3*x^2}; %\addlegendentry{(3x^2)} \end{axis} \end{tikzpicture}

enter image description here

xpt
  • 46
  • With restrict y to domain, the plots will end at different heights (the last sampled point within this domain). axis line style={-to} changes the arrow tip!? – hpekristiansen Jan 05 '23 at 05:24
  • @hpekristiansen axis line style={-to} will change the arrow tip. Regarding restrict y to domain, that's true, that's why I added some many samples points. With samples at = {} you can modify the samples points – xpt Jan 05 '23 at 05:39
  • Yes, but why are you changing the arrow tip? – hpekristiansen Jan 05 '23 at 05:46
  • 1
    because I thought the OP wanted that – xpt Jan 05 '23 at 05:59
0
\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=8cm, height=10cm,
xmin=-20, xmax=20,
ymin=0, ymax=40,
axis lines=middle, axis line style=thick,
xlabel=$x$, ylabel=$f(x)$,
xlabel style={anchor=west}, ylabel style={anchor=south},
enlarge x limits={abs=2}, enlarge y limits=0.1,
]
\clip (-20,0) rectangle (20,40);
\addplot[
red!90!black, ultra thick,
domain=-20:20, 
smooth
] {0.5*x^2};
\addplot[
green!50!black, ultra thick,
domain=-20:20, 
smooth,
] {0.1*x^2};
\addplot[
blue!80!black, ultra thick,
domain=-20:20, 
smooth
] {0.05*x^2};
\end{axis}
\end{tikzpicture}
\end{document}

Graph with three parabolas

You can also use the option axis line style={shorten >=-10pt, shorten <=-10pt} to simply draw the axis lines longer. This has some limitations - no ticks in this extension, no grid, and manual adjustment of e.g. labels are needed.

  • Thanks! I am curious to learn, what if you want the line under x-axis to be bit smaller, while keeping the axis above the x-axis? I was thinking changing your enlarge y limits into like enlarge y limits={value=0.1,upper}, then what should I write to get one under x-axis with a smaller distance? – UnknownW Jan 05 '23 at 11:07
  • That is not supported - see https://tex.stackexchange.com/questions/30437/asymmetric-enlarge-x-limits-in-pgfplots . You could enlarge the upper limit and then negative shorten the lower of the axis line. enlarge y limits={value=0.1, upper}, y axis line style={shorten <=-5pt} – hpekristiansen Jan 05 '23 at 11:26