How do I plot an ellipse with \addplot with using an equation
Solution 1
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
]
\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}
Solution 2
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
]
\addplot [domain=-pi:pi,samples=200,red,line width=0.7pt]({3*sin(deg(x))}, {sqrt(3)*cos(deg(x))});
\end{axis}
\end{tikzpicture}
\end{document}
This is the result running in Gummi.
- a single \addplot was used
- the graph renders well everywhere
- the sample size is much smaller
EDIT: code modified in response to marmot's comments below.
Related videos on Youtube
God bless
Updated on December 10, 2020Comments
-
God bless almost 3 years
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 andy
's in equations. Fine, I'll just express they
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 firstaddplot
. 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:
- of the need for graphing two graphs for a single ellipse,
- of the fact that the graph doesn't render near antipodal points (of origin),
- 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.-
Torbjørn T. almost 5 yearsAnother option is tex.stackexchange.com/questions/18359/…
-
Admin almost 5 yearsPerhaps use
domain=-pi:pi
and it is not necessary to loadtikz
afterpgfplots
. -
DJP almost 5 yearsGood point about
tikz
afterpgfplots
; I also don't need thecalc
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.