# How do I plot an ellipse with \addplot with using an equation

i am trying to plot an ellipse in my coordinate system. TikZ provides an implemented easy method for plotting ellipses, but I don't like it — I want to draw my ellipses with actual analytic equations.

When I tried \addplot[line width=0.7pt, red, samples=2000]{(x^2)/9 + (y^2)/3 == 1};, I got this error message:

! Package pgfplots Error: Sorry, you can't use 'y' in this context. PGFPlots expected 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.


I took the advice and added [mesh], [scale y>0] and [y!=0:0] to my \addplot preamble. Well, it didn't go well, as it produced this error:

! Package pgfplots Error: Sorry, the supplied plot command is unknown or unsupported by pgfplots! Ignoring it..


Well, looks like pgfplots doesn't like and y's in equations. Fine, I'll just express the y and proceed from there. From this process, I got \addplot[line width=0.7pt, red, samples=2000]{-sqrt(9 - x^2)/sqrt(3)};, which did render, but only the lower part of the ellipse. This is to be expected, as the program is only taking either negative or positive part of the square root argument when drawing the first addplot. The solution is to simply add a - to the equation (or in this case, remove it, sice the original equation already had a -) in the next \addplot to force the program to finally (kind of) render the complete ellipse, which brings me to my MWE:

\documentclass{standalone}

\usepackage{pgfplots}
\usepackage{tikz}

\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
xmin=-5, xmax=5,
grid=both,
axis lines=middle,
minor tick num=9,
axis line style={latex-latex},
ticklabel style={font=\tiny},
axis equal
]

\addplot[line width=0.7pt, red, samples=2000]{-sqrt(9 - x^2)/sqrt(3)};
\addplot[line width=0.7pt, red, samples=2000]{sqrt(9 - x^2)/sqrt(3)};

\end{axis}
\end{tikzpicture}

\end{document}


Which produces:

I am displeased with my result because:

1. of the need for graphing two graphs for a single ellipse,
2. of the fact that the graph doesn't render near antipodal points (of origin),
3. of the fact that I have to set a very large samples value to get even an arguably decent result (if I don't, the gap between antipodal points will be even bigger).

Please, help me plot an ellipse with \addplot by means of a single equation. Thank you in advance.

Can use polar coordinates to plot an ellipse. For equation, read polar form of ellipse.

This is just a minimum working example. Parameter values has to be adjusted to match exact requirement.

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
%\usepackage{tikz}
%\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-5, xmax=5,
grid=both,
axis lines=middle,
minor tick num=9,
axis line style={latex-latex},
ticklabel style={font=\tiny},
axis equal
]

line width=0.7pt, red] (x,{1/(sqrt(0.3-0.2*cos(x)*cos(x)))});
\end{axis}
\end{tikzpicture}
\end{document}


These plotting programs are typically for plotting functions, which an ellipse isn't. Besides breaking the relation into two functions, as you've done, it's also possible (and in fact works better to avoid needing so many sample points) to define the ellipse as parametric equations; see the section on converting ellipses. You have to know that deg(x) is needed for syntax to get radians and the domain has to cover a range of 2pi; I've got it going from -pi to pi but 0 to 2pi also works. To make sure less points are needed I extended to -3.4 to 3.4

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-5, xmax=5,
grid=both,
axis lines=middle,
minor tick num=9,
axis line style={latex-latex},
ticklabel style={font=\tiny},
axis equal
]
\end{axis}
\end{tikzpicture}

\end{document}


This is the result running in Gummi.

1. a single \addplot was used
2. the graph renders well everywhere
3. the sample size is much smaller

EDIT: code modified in response to marmot's comments below.

• Perhaps use domain=-pi:pi and it is not necessary to load tikz after pgfplots.
– user121799
Dec 9 '18 at 10:20
• Good point about tikz after pgfplots; I also don't need the calc library. I've changed domain to -pi:pi but not sure what happened yesterday when using a numerical approximation for pi didn't work well. Maybe I mistyped the decimal.
– DJP
Dec 9 '18 at 17:16