# Pgfplot/tikz plot creation

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{figure}
\centering
\resizebox{\textwidth}{!}{%
\begin{tikzpicture}
\begin{axis}[
axis lines=middle,
xmin=-10, xmax=15,
ymin=-200, ymax=260,
xlabel=$x$, xlabel style={below right},
ylabel=$y$, ylabel style={above left},
xtick={-10,1,15},
ytick={-200, 25, 250},
tick style={thick},
]
\end{axis}
\end{tikzpicture}
}%
\end{figure}

\end{document}


When i enter this code i get this:

But what im really going for is something like this:

Which means that i have a few problems, that i can't figure out how to solve.

1. The function that i'm trying to plot(x^3 - 9x^2 - 30x + 200) doesn't fully render. I'm not sure, what to do about this.
2. I would like to make "ghost ticks" on the axes, that are visible, but doesn't have a number attached to them. Also, i would like to display the numbers 250 and 15 without the "ticks" being on top of the arrows.
3. If i were to make the numbers on the axis smaller, how would i do that?

I'm sorry if the answers to this is obivous, i have only recently started using LaTeX.

New Question: This is such a small thing. I don't want to create an entirely new post just for this.

This is my current code

\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
\begin{axis}[width=14cm,
axis lines=middle,
grid=major,
xmin=-5, xmax=50.5,
ymin=-2, ymax=10.5,
xlabel=$x$, xlabel style={right},
ylabel=$y$, ylabel style={above},
tick style={thick},
ticklabel style={font=\normalsize},
xtick={0,5,...,50}, extra x ticks={-5}, extra x tick style={grid=none},
ytick={0,2,...,10}, minor ytick={-1,1,...,9},extra y ticks={-2}, extra y tick style={grid=none},
legend entries={0.5x},
legend style={
at={(0.8,0.937)},
anchor=north,
legend columns=1},
legend cell align={left}
]
\legend{$f(x)=\sqrt{x} \; {,} \; x \geq 0$ , $g(x)=0.5x$}
\end{axis}
\end{tikzpicture}
\end{figure}


It shows this:

My question: Why doesn't the graph sqrt(x) fully show/ go all the way to (0,0)?

• Just add domain=-10:15 to your \addplot options it specifies the plot x-range. Some side notes : it is not necessary to use tikz before pgfplots as the latter already does the job for you. And if you want an axis of width \textwidth, just add width=\textwidth to the axis options. See pgfplots manual for all options ! Oct 5 '19 at 14:16
• You're saying that i dont need to add the \usepackage{tikz}? Oct 5 '19 at 14:49
• This site is not made for evolving questions. The reason why your plot does not "fully render" is that the points where you take the square root of a negative number are discarded. To plot the square root all the way down to zero, make sure that 0 is among the samples. This can be achieved e.g. with \addplot[blue,thick,samples=111,domain=-5:50,smooth] {sqrt(x)};. Note that I am not supposed to add this information here. Rather, you are supposed to ask separate questions for follow-up questions.
– user194703
Oct 9 '19 at 19:10
• Alright, i had no idea that was how it worked. I'm sorry! I assume that i can still ask questions about your answers here. How do i know that i need samples=111 exactly? When i try 112, 200 and other numbers it does not work. Oct 9 '19 at 19:21
• Why 111? Because it satisfies \exists n \in\mathbb{N}: n*(50+5)+1=111. This is the condition that a sample hits zero. The samples are take equidistantly between -5, the lower end of the domain, and 50, the upper end. So if you take 56 samples, the first one will be at -5, the next one at -4 ... and you hit 0. If you take 111, the first one will again be at -5, the next one at -4.5 ... and you hit 0. However, with samples=100 you miss 0.
– user194703
Oct 10 '19 at 2:42

The "full rendering" of the plot can be achieved by adding an appropriate domain. The ghost ticks are minor ticks. The numbers can be made smaller by adding ticklabel style={font=\tiny}. The width of the plot can be controlled using the width key, do not use \resizebox. Also it is recommended to add the compat key.

\documentclass[11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[width=\textwidth,
axis lines=middle,
xmin=-10, xmax=15.5,
ymin=-200, ymax=260,
xlabel=$x$, xlabel style={below right},
ylabel=$y$, ylabel style={above left},
xtick={-10,1,15},
ytick={-200, 25, 250},
tick style={thick},
minor xtick={-9,-8,...,14},
minor y tick num=10,
ticklabel style={font=\tiny}
]
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}


ADDENDUM: Plotting a stepwise defined function.

\documentclass[11pt,fleqn]{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
Suppose we want to plot the function
$f(x)=\begin{cases} \frac{x^2}{2}-2x+3\;, & x\le 4\;,\\ x-1\;, & x>4\;. \end{cases}$
Then one can declare a function using the ifthenelse function,
\begin{quote}
\verb|declare function={f(\x)=ifthenelse(\x<=4,0.5*\x^2-2*\x+3,\x-1);}|\;.
\end{quote}
The result is shown in Figure~\ref{fig:f}.

\begin{figure}[htb]
\centering
\begin{tikzpicture}[declare
function={f(\x)=ifthenelse(\x<=4,0.5*\x^2-2*\x+3,\x-1);}]
\begin{axis}[width=\textwidth,
axis lines=middle,
xmin=-10, xmax=15.5,
xlabel=$x$, xlabel style={below right},
ylabel=$f(x)$, ylabel style={above left},
xtick={-10,1,15},
tick style={thick},
minor xtick={-9,-8,...,14},
minor y tick num=1,
ticklabel style={font=\tiny}]
\end{axis}
\end{tikzpicture}
\caption{$f(x)$.}
\label{fig:f}
\end{figure}
\end{document}


• Thank you! It worked! :) Oct 5 '19 at 14:45
• New problem: I want to add the declare the function ´f(x)=sqrt(x), x>=0´ How can i do this? I really dont get the adding functions system in pgfplots :b Oct 9 '19 at 15:54
• @Nikolai Assuming you want 0 for x<0 you need declare function={f(\x)=ifthenelse(\x>=0,sqrt(\x),0);}. This is equivalent to declare function={f(\x)=sqrt(max(\x,0));}.
– user194703
Oct 9 '19 at 16:00
• I have updated the question at the top, can you take a look at it? :b It's only the part under New Question:. Oct 9 '19 at 17:49