1

I would like to plot the graph of a simple equation like x^2+xy+2y^2=1 using TikZ. There are some posts on implicit plots here, but nothing worked in my case.

I am using TeXShop Version 3.92 and \documentclass{book}.

Heiko Oberdiek
  • 271,626
  • 2
    What does nothing worked mean? What have you tried so far? Please add a MWE. You might want to have a look at pgfplots. – TeXnician Dec 09 '17 at 13:22
  • I tried what is proposed in 18359 and got an error message. The method proposed in 18359 worked, but I was not able to modify it to work for the type of equations like the one in my question. The example given in 285246, which I liked the most, texed without any problems, but then I got an empty page. – Piotr Mikusinski Dec 09 '17 at 13:39
  • did you enable shell-escape as in 285246 ? – NuminousName Dec 09 '17 at 13:41
  • My preferences do not look like what they show. The closest thing is Pdftex under Default Script. There are no additional options. It is checked. – Piotr Mikusinski Dec 09 '17 at 13:47

4 Answers4

3

Question 18359

The answer of question 18359 works for me. Changed line:

splot x^2 + x*y + 2*y^2 - 1;

And I added the line

set samples 500;

for a smoother curve.

Full example:

\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;
      set samples 500;
      splot x^2 + x*y + 2*y^2 - 1;
    };
  \end{axis}
\end{tikzpicture}

\end{document}

This solution requires that gnuplot is installed and available on the command line. Depending on the TeX configuration (texmf.cnf in TeX Live), usually option -shell-escape (TeX Live)/-enable-write18 (MiKTeX) for the TeX compiler is needed to run the example, see the comment of Sari.

Result

Question 285246

The example of question 285246 also works with the following function:

f(x,y) = x**2 + x*y + 2*y**2 - 1;

(x2 is expressed as x**2.)

Full example:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw plot[id=question1, raw gnuplot] function{
    f(x,y) = x**2 + x*y + 2*y**2 - 1;
    set xrange [-4:4];
    set yrange [-4:4];
    set view 0,0;
    set isosample 1000, 1000;
    set size square;
    set contour base;
    set cntrparam levels incre 0,0.1,0;
    unset surface;
    splot f(x,y);
  };
\end{tikzpicture}
\end{document}

Result

Heiko Oberdiek
  • 271,626
  • I just can't get the ellipse in my TeXShop Version 3.92. I tried to enable shell-escape, but for some reason it does not seem to work. – Piotr Mikusinski Dec 09 '17 at 15:52
  • @PiotrMikusinski Check the .log file, it should contain something like runsystem(gnuplot test.pgf-plot.gnuplot)...executed.. – Heiko Oberdiek Dec 09 '17 at 15:57
  • I found this in my log file: \openout3 = `"DAPM-13-SA-Quadratic forms.question1.gnuplot"'.

    runsystem(gnuplot "DAPM-13-SA-Quadratic forms.question1.gnuplot")...executed.

    – Piotr Mikusinski Dec 09 '17 at 16:12
  • @PiotrMikusinski That means that shell escape is working. Now, check the console output for errors, e.g. system returned with code 256. Unhappily, this is not written to the .log file. I do not know, whether TeXShop is able to catch it. Therefore, it is better to run on the command line, e.g. pdflatex -shell-escape test and check the messages on the screen. – Heiko Oberdiek Dec 09 '17 at 16:28
  • I also get this: Package pgf Warning: Plot data file Ellipse.question1.table' not found. on input line 20. This is what I got when I run "pdflatex -shell-escape test": This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex) \write18 enabled. entering extended mode ! I can't find filetest'. <*> test – Piotr Mikusinski Dec 09 '17 at 16:45
  • @PiotrMikusinski That means, the gnuplot command did not succeed (the output file was not generated). Before the warning there can be a message, warning, error with the reason. Further tests: gnuplot --version should print the version number of gnuplot. Also, there should be a file Ellipse.question1.gnuplot, the source code for gnuplot. Test: gnuplot Ellipse.question1.gnuplot on the command line. – Heiko Oberdiek Dec 09 '17 at 17:00
  • Finally I found something that worked for me (I used more mathematics than LATEX): \begin{tikzpicture} \begin{axis}[hide axis, xmin=-2,xmax=2, ymin=-2,ymax=2, ] \addplot blue,thick,domain=0:360,samples=200; \end{axis} \end{tikzpicture} – Piotr Mikusinski Dec 10 '17 at 16:10
1

What about this?

\documentclass{article}
\usepackage{pgfplots} % It is based on tikz!
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot3[
surf,
samples=20,
domain=-100:100,
y domain=-100:100
] 
{x^2+x*y+2*y^2-1};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • 3
    This is not what I asked for in my question (I asked for the equation x^2+xy+2y^2=1 that gives us an ellipse), but I may need the 3d version too. Can we get the surface without the axes? – Piotr Mikusinski Dec 09 '17 at 15:47
  • Oh really, sorry:(((( –  Dec 09 '17 at 16:34
  • If you don't want axes, then you should use hide axis option of axis environment. –  Dec 09 '17 at 17:42
0
\begin{tikzpicture}
    \begin{axis}[hide axis,xmin=-2,xmax=2,ymin=-2,ymax=2,]
    \addplot [blue,thick,domain=0:360,samples=200]({(cos(x)+sin(x)},{sin(x)});
    \end{axis}
\end{tikzpicture}
Dai Bowen
  • 6,117
0

If you don't mind a not-tikz solution, the mfpic package, interface to the MetaPost language, seems to work quite well in this kind of problem with its \levelcurve command.

\documentclass[border=3bp]{standalone}
\usepackage[metapost, mplabels, truebbox]{mfpic}
  \setlength{\mfpicunit}{1cm}
  \opengraphsfile{\jobname}
\begin{document}
\begin{mfpic}[2]{-1.5}{1.5}{-1.5}{1.5}
  \drawcolor[gray]{0.7}
  \gridlines{.5,.5}
  \drawcolor{black}
  \levelcurve{origin, .01}{x**2 + x*y + 2(y**2) < 1}
  \doaxes{xy}
  \tlpointsep{3bp}
  \tlabels{[tc]{(-1, 0)}{$-1$} [tc]{(1, 0)}{$1$} [tc]{(\xmax, 0)}{$x$}}
  \tlabels{[cr]{(0, -1)}{$-1$} [cr]{(0, 1)}{$1$} [cr]{(0, \ymax)}{$y$}}
\end{mfpic}
\closegraphsfile
\end{document}

With TeXShop, process this TeX file with (PDF)LaTeX, then with MetaPost, and finally with LaTeX again.

enter image description here

See the mfpic documentation p.44-45 for more information on this most useful command.

Franck Pastor
  • 18,756