1

I have some problems to find the right options in the 566 pages of the manual. In the plot

Output of the MWE

I would like to achieve that

  • the curve is smoother (increasing the number of samples results in the error "Dimension too large");
  • the labels x and y are placed below and left of the axes, respectively;
  • the gray grid is shown across the whole coordinate system with a distance of 1 unit in each direction although there is only one tick and one label on each axis.

This is the code that produces the above image.

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\pgfplotsset{compat=1.15}
% \pgfplotsset{samples=512}

\begin{document}

\begin{tikzpicture}
\begin{axis}
[
  grid = major,
  axis x line = middle, axis y line = middle,
  xlabel = $x$, xmin = -6, xmax = 6, xtick = {1}, 
  ylabel = $y$, ymin = -3, ymax = 6, ytick = {1},
]
\addplot[domain = -6 : 6] {-2 * (x + 3) * (x - 3) / x^2};
\end{axis}
\end{tikzpicture}

\end{document}
3

See comments in the code for ticks and grid settings.

You can use smooth to make the plot smoother in this case, but in general be a bit careful with it. See How to create a Ricker Wavelet in TikZ and pgfplots, overshooting problem for examples of smooth gone bad.

output of code

\documentclass{article}

\usepackage{pgfplots} % loads tikz
\pgfplotsset{compat=1.15}
\begin{document}

\begin{tikzpicture}
\begin{axis}
[
  grid = major,
  axis x line = middle, axis y line = middle,
  xlabel = $x$, xmin = -6, xmax = 6,
  % xticks every 1 unit, remove all labels
  xtick distance = 1, xticklabels={},
  ylabel = $y$, ymin = -3, ymax = 6,
  % yticks every 1 unit, remove all labels
  ytick distance = 1, yticklabels={},
  % append below left to styles of both axis labels
  ylabel style={below left}, xlabel style={below left},
  % add extra ticks at 1, which will have labels
  extra x ticks={1},extra y ticks={1},
  % we get extra grid lines for the extra ticks, disable those and make ticks black
  extra x tick style={grid=none,tick style=black},
  extra y tick style={grid=none,tick style=black},
  % black instead of grey ticks
  tick style={black},
]
% add smooth 
\addplot[domain = -6 : 6, smooth] {-2 * (x + 3) * (x - 3) / x^2};
\end{axis}
\end{tikzpicture}

\end{document}
  • Almost perfect. It seems, however, that the grid is drawn on top of the ticks such that they are not clearly visible. Both, SumatraPDF and Adobe Acrobat Reader, show it this way. Can this be fixed? – Matthias Dec 11 '17 at 21:59
  • @Matthias The grid is below, but the ticks are grey. tick style={black} makes them black. Needs some extra stuff for the extra ticks, will edit shortly. – Torbjørn T. Dec 11 '17 at 22:09

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.