[Tex/LaTex] How to plot an ellipse with \addplot with using an equation

graphspgfplotstikz-pgf

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:

enter image description here

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.

Best Answer

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

enter image description here

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
]

\addplot[domain=0:360,data cs=polar, samples=200,
line width=0.7pt, red] (x,{1/(sqrt(0.3-0.2*cos(x)*cos(x)))});
\end{axis}
\end{tikzpicture}
\end{document}