22

What is the correct way to plot an implicit function using pgfplots? Consider the following example:

\documentclass{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \begin{axis}
    \addplot+[mark=none] function[raw gnuplot] {
      set contour base;
      set cntrparam levels discrete 0.0;
      unset surface;
      set view map;
      set isosamples 500;
      splot exp(x)*cos(y)-1-x;
    };
  \end{axis}
\end{tikzpicture}

\end{document}

This is what the function looks like in gnuplot:

expected result

pgfplots, however, connects function's segments, which is unexpected:

incorrect pgfplots result

5
  • 1
    The development version of PGFPlots includes an empty line option that can be set to jump to get the desired behaviour. If you're using TeXLive, you can update PGFPlots through tlmgr by temporarily using the repository http://tlcontrib.metatex.org/2010.
    – Jake
    May 16, 2011 at 16:41
  • @Jake: Consider posting this as an answer. Using the empty line option to allow interrupted plots works very well except gaps between segments become visible: i.imgur.com/8k3pp.png. Is there a way to fix this?
    – caustic
    May 16, 2011 at 20:28
  • I'm afraid not, because that's just the data gnuplot generates (the gaps are also visible in the gnuplot screenshot in your question). If you increase the line width to thick and plot the contour for 0.003 instead of 0.0, it looks okay.
    – Jake
    May 16, 2011 at 20:59
  • See also graphics - Plotting an implicit function using TikZ - TeX - LaTeX Stack Exchange for other options/libraries of plotting implicit function in TeX.
    – user202729
    Jun 12, 2022 at 11:36
  • The question explicitly asks for a pgfplots solution. Different libraries are irrelevant.
    – caustic
    Jun 13, 2022 at 18:31

1 Answer 1

19

The development version of PGFPlots includes an empty line option that can be used to influence the behaviour. It is set to jump by default, which breaks the plot when an empty line is encountered. If you're using TeXLive, you can update PGFPlots through tlmgr by temporarily using the repository http://tlcontrib.metatex.org/2010.

The gap at the intersections is a numerical artefact introduced by gnuplot. It can't be fixed completely, but by plotting with a thicker line, and choosing a slightly different contour line to be plotted, it can be glossed over:

\documentclass{article}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
  \begin{axis}
    \addplot +[no markers,
      raw gnuplot,
      thick,
      empty line = jump % not strictly necessary, as this is the default behaviour in the development version of PGFPlots
      ] gnuplot {
      set contour base;
      set cntrparam levels discrete 0.003;
      unset surface;
      set view map;
      set isosamples 500;
      splot exp(x)*cos(y)-1-x;
    };
  \end{axis}
\end{tikzpicture}

\end{document}

pgfplots/gnuplot contour plot

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .