[Tex/LaTex] How to plot an implicit function in pgfplots without having to use gnuplot

pgfplots

I have been trying to plot a couple of really simple implicit linear functions using only pgfplots internal facilities.

Here is my MWE:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{external}
\tikzexternalize
\pgfplotsset{compat=newest}
\usepackage{amsmath}
\usepackage[T1]{fontenc}

\begin{document}

\begin{tikzpicture}

\begin{axis}
\addplot[color=red]{3*x + 2*y - 2};
\end{axis}

\end{tikzpicture}

\end{document}

When I run this, I get this error:

! Package pgfplots Error: Sorry, you can't use 'y' in this context. PGFPlots ex
pected to sample a line, not a mesh. Please use the [mesh] option combined with
[samples y>0] and [domain y!=0:0] to indicate a twodimensional input domain.
See the pgfplots package documentation for explanation.

This particular function can be made explicit, but I'd still like to know if it is possible to plot implicit functions with pure pgfplots. All the examples I have seen thus far call gnuplot.

Best Answer

Actually, it is possible to draw implicit functions with LaTeX, if not with PGFPlots… but also in that case you need to cheat a little. Dan Luecking has implemented in its mfpic package, which is a (La)TeX interface to MetaPost (or METAFONT), a macro called

\levelcurve[spec]{seed,step} {inequality},

which works quite well beyond certain conditions, as said in the documentation, p. 44-45:

This figure macro produces a level curve of some function F(x;y). There are three requirements on the parameters for this to work correctly. First, in order to obtain the curve satisfying F(x;y) =C, the {inequality} must be either {F(x,y) > C} or {F(x,y) < C}. Second, the level curve must surround the point given by the seed paramter, and third, the inequality must be true at this seed point.

The command works by searching rightward from seed until it encounters the first point on the level curve. It then tries to find a nearby point on the level curve and joins it to the first one, and continues similarly until it finds it has returned near the starting point. The meaning of “nearby point on the level curve” is the intersection of the level curve with a circle of radius step centered at the previously found point. If the region defined by the inequality extends beyond the bounds of the picture (as set by the \mfpic command), the region is truncated and the resulting curve will follow along the picture’s border.

I've already used this macro to answer a similar question, and it worked very well in that case. I've just applied it on your simple example:

\documentclass{standalone}
\usepackage{amsmath}
\usepackage[metapost]{mfpic}
  \setlength{\mfpicunit}{1cm}
  \opengraphsfile{\jobname}
\begin{document}
\begin{mfpic}[2]{-0.5}{2}{-0.5}{2}
  \levelcurve[p]{(0, 0), 0.01}{3*x + 2*y - 2 < 0}
  \doaxes{xy}
  \tlpointsep{2bp}
  \tlabels{[tr](0, 0){$O$} [tr](0.667, 0){$\dfrac{2}{3}$} [cr](0, 1){$1$} 
    [tc](\xmax, 0){$x$} [cr](0, \ymax){$y$}}
\end{mfpic}
\closegraphsfile
\end{document}

Typeset with LaTeX, then with MetaPost, and then with LaTeX again. The result is:

enter image description here

(As indicated in the documentation, the borders of the graph have been used to close the line which was the expected result.)

Still, as Paul Gessler noticed, an external program has worked behind the scene, namely MetaPost (be it as close to LaTeX as an external program can be).