3

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.

6
  • @sudosensei I've been trying it with \addplot3[mesh,samples=10,domain y=-6:4]{3*x + 2*y - 2}; now. I get a 3D plot with intersecting planes (there is another similar function that isn't in the MWE). I'm tinkering with the view settings to see if that gives me what I want, but still no luck. Commented Feb 4, 2014 at 23:10
  • @sudosensei I don't think this is the right approach, because these are supposed to be implicit functions in R, and not two-dimensional functions z = f(x,y). Commented Feb 4, 2014 at 23:16
  • Well, plotting implicit functions take either an equation solver or a lot of memory and working time. It's overkill to want to do these things directly in LaTeX. Well, IMHO it's overkill to draw any functions in LaTeX, but that's another story. Besides gnuplot, you can try to use sagetex too.
    – yo'
    Commented Feb 4, 2014 at 23:26
  • Oh, I see. I seem to have misunderstood the question. Unfortunately, I am not a TikZ/pgfplots guru, so you'll just have to wait for the cavalry. :-) Is there a reason you want to avoid gnuplot though.? As far as I know, all the call to gnuplot does is export a data file that is plotted by pgfplots. So the plot you'd get would be identical to one produced directly with pgfplots internals.
    – sudosensei
    Commented Feb 4, 2014 at 23:28
  • 1
    I believe pgfplots uses gnuplot internally to generate the data points for the contour. If memory serves me right, the command you are looking for is \addplot3[contour gnuplot,...].
    – sudosensei
    Commented Feb 4, 2014 at 23:39

2 Answers 2

2

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).

1

No, it is not currently possible to plot implicit functions natively using pgfplots.

The use of gnuplot (or some other external computation engine) is required for these types of plots at this time. Even contour plots in pgfplots require gnuplot or a sufficiently prepared data file.

You must log in to answer this question.

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