1

I am trying to plot a graph with tikzpicture and created two variants, one using \begin{axis} with \addplot and another one with \draw plot (which I prefer because the grid looks better and it is better scalable).

While the code with \begin{axis} does work:

\usepackage{amsmath}
\usepackage{pgfplots}   % Needed for "\begin{axis}"
\usepackage{tikz}

\begin{tikzpicture}[scale=1.0]
\begin{axis}
[
  axis x line=center,
  axis y line=center,
  xtick={-4,...,4},
  ytick={-4,...,4},
  xlabel={$x$},
  ylabel={$y$},
  xlabel style={below right},
  ylabel style={above left},
  xmin=-4.4,
  xmax=4.4,
  ymin=-4.4,
  ymax=4.4
]
\addplot [mark=none,domain=-3:3, smooth] {x^4) - 4*(x^2) + 2};

\end{axis}
\end{tikzpicture}

and produces an output like:

graph using addplot
The following code produces some kind of "out of range"-graph:

\begin{center}
\begin{tikzpicture} [scale=0.8] 

% COORDINATE-SYSTEM
  % draw grid
  \draw[step=0.5, gray!20] (-5,-5) grid (5,5);
  % enumerate rows
  \foreach \y in {-5,...,5} {\node[text=gray!50, left]  at (0,\y) {$\y$};}
  % enumerate cols
  \foreach \x in {-5,...,5} {\node[text=gray!50, below] at (\x,0) {$\x$};}
  % draw horizontal line
  \draw[gray, ->] (-5,0) -- (5,0) node[black, right] {$x$};
  % draw vertical line
  \draw[gray, ->] (0,-5) -- (0,5) node[black, above] {$y$};

% FUNCTION
  % Syntax: \draw [domain=<xmin>:<xmax>] plot (\x, {function});
  \draw [smooth, domain=-4:4] plot (\x, {(\x^4) - 4*(\x^2) + 2});

\end{tikzpicture} 
\end{center}

As you can see here:

enter image description here

So how can i fix the second graph?
I am grateful for any help, links, references and hints!

  • Welcome to TeX - LaTeX! 4^4 = 256 so your function has large values. You need reduce the y interval. – Andrew Swann Nov 12 '17 at 15:43
  • @AndrewSwann I reduced the domain to -2.15:2.15 and the graph is now visible, but the function is only displayed correctly on the positive side of the x-axis, on the negative side it is flipped upside down^^ – PatrickSteiner Nov 12 '17 at 16:00
3

pgfplots automatically clips the plot to the plot area. tikz's \draw plot does not. But you can add \clip (-5,-5) rectangle (5,5); before it.

In the example below I also took the freedom to make both plots look the same by adding some options to \begin{axis} (see comments).

\documentclass[border=1mm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}   % Needed for "\begin{axis}"
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\begin{axis}
[
  % set size of plot
  width=8.8cm,
  height=8.8cm,
  scale only axis,
  axis x line=center,
  axis y line=center,
  xtick={-4,...,4},
  ytick={-4,...,4},
  xlabel={$x$},
  ylabel={$y$},
  xlabel style={below right},
  ylabel style={above left},
  xmin=-4.4,
  xmax=4.4,
  ymin=-4.4,
  ymax=4.4,
  % add grid
  grid=both,
  % minor grid has one line between major grid lines
  minor tick num=1,
  % change color of grid
  grid style={gray!20},
  % change color of tick labels
  tick label style={gray!50},
  % no tick lines
  major tick length=0pt,
  minor tick length=0pt,
  % set color and arrow tip for axes
  axis line style={gray,->},
]
\addplot [mark=none,domain=-3:3, smooth] {(x^4) - 4*(x^2) + 2};

\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
% COORDINATE-SYSTEM
  % draw grid
  \draw[step=0.5, gray!20] (-4.4,-4.4) grid (4.4,4.4);
  % enumerate rows
  \foreach \y in {-4,...,4} {\node[text=gray!50, left]  at (0,\y) {$\y$};}
  % enumerate cols
  \foreach \x in {-4,...,4} {\node[text=gray!50, below] at (\x,0) {$\x$};}
  % draw horizontal line
  \draw[gray, ->] (-4.4,0) -- (4.4,0) node[black, right] {$x$};
  % draw vertical line
  \draw[gray, ->] (0,-4.4) -- (0,4.4) node[black, above] {$y$};

% FUNCTION
  % clip to graph dimensions
  \clip (-4.4,-4.4) rectangle (4.4,4.4);
  % Syntax: \draw [domain=<xmin>:<xmax>] plot (\x, {function});
  \draw [smooth, domain=-3:3] plot (\x, {((\x)^4) - 4*((\x)^2) + 2});
  % added () around \x to make plot work  ^  ^         ^  ^
\end{tikzpicture} 
\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.