-
Notifications
You must be signed in to change notification settings - Fork 3
/
conics-matplotlib.html
256 lines (232 loc) · 15.5 KB
/
conics-matplotlib.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE html>
<html lang="en" itemscope itemtype="http://schema.org/Article">
<head>
<title>Drawing conics in Matplotlib</title>
<meta charset="utf-8">
<meta property="og:title" content="Drawing conics in Matplotlib">
<meta property="og:site_name" content="Modesto Mas | Blog">
<meta property="og:image" content="https://mmas.github.io/images/profile.jpg">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<meta property="og:url" content="https://mmas.github.io/conics-matplotlib">
<meta property="og:locale" content="en_GB">
<meta name="twitter:image" content="https://mmas.github.io/images/profile.jpg">
<meta name="twitter:url" content="https://mmas.github.io/conics-matplotlib">
<meta name="twitter:card" content="summary">
<meta name="twitter:domain" content="mmas.github.io">
<meta name="twitter:title" content="Drawing conics in Matplotlib">
<meta name="description" content="Plotting conic sections in Matplotlib may seem easy, but it can get complicated if we use matplotlib.pyplot.plot. Especially if we attempt o draw a co...">
<meta name="twitter:description" content="Plotting conic sections in Matplotlib may seem easy, but it can get complicated if we use matplotlib.pyplot.plot. Especially if we attempt o draw a co...">
<meta property="og:description" content="Plotting conic sections in Matplotlib may seem easy, but it can get complicated if we use matplotlib.pyplot.plot. Especially if we attempt o draw a co...">
<meta name="keywords" content="algebraic-geometry,geometry,matplotlib,numpy,python">
<meta property="og:type" content="blog">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:type" content="article">
<meta property="article:author" content="https://github.com/mmas">
<meta property="article:section" content="algebraic-geometry">
<meta property="article:tag" content="algebraic-geometry,geometry,matplotlib,numpy,python">
<meta property="article:published_time" content="2016-04-21">
<meta property="article:modified_time" content="2016-04-21">
<link rel="stylesheet" type="text/css" href="/css/main.css">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
CommonHTML: {
scale: 93,
showMathMenu: false
},
tex2jax: {
"inlineMath": [["$","$"], ["\\(","\\)"]]
}
});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</head>
<body class="entry-detail">
<header>
<div>
<img src="https://mmas.github.io/images/profile.jpg">
<a class="brand" href="/">Modesto Mas</a>
<span>Data/Python/DevOps Engineer</span>
<nav>
<ul>
<li><a href="/tags">Tags</a></li>
<li><a href="https://github.com/mmas/mmas.github.io/issues" target="_blank">Issues</a></li>
</ul>
</nav>
</div>
</header>
<section id="content" role="main">
<article>
<header>
<h1><a href="/conics-matplotlib">Drawing conics in Matplotlib</a></h1>
<time datetime="2016-04-21">Apr 21, 2016</time>
<a class="tag" href="/tags?tag=algebraic-geometry">algebraic-geometry</a>
<a class="tag" href="/tags?tag=geometry">geometry</a>
<a class="tag" href="/tags?tag=matplotlib">matplotlib</a>
<a class="tag" href="/tags?tag=numpy">numpy</a>
<a class="tag" href="/tags?tag=python">python</a>
</header>
<aside id="article-nav"></aside>
<section class="body">
<p>Plotting <a target="_blank" href="https://en.wikipedia.org/wiki/Conic_section">conic sections</a> in <a target="_blank" href="http://matplotlib.org/">Matplotlib</a> may seem easy, but it can get complicated if we use <a target="_blank" href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot"><code>matplotlib.pyplot.plot</code></a>. Especially if we attempt o draw a conic not in its standard position (except for the hyperbola). There are some tricks we can use, like plotting in two parts for circles, ellipses and parabolas, and <a target="_blank" href="http://docs.scipy.org/doc/numpy/reference/maskedarray.html">masking</a> values where the function is not defined for hyperbolas.</p>
<p>Instead we can create a two-dimensional Cartesian plane and draw the contour corresponding to the conic algebraic function.</p>
<p>Let's star importing the modules and setting some aesthetic properties:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="ch">import</span> numpy <span class="ch">as</span> np
<span class="ch">import</span> matplotlib <span class="ch">as</span> mpl
<span class="ch">import</span> matplotlib.pyplot <span class="ch">as</span> plt
mpl.rcParams[<span class="st">'lines.color'</span>] = <span class="st">'k'</span>
mpl.rcParams[<span class="st">'axes.prop_cycle'</span>] = mpl.cycler(<span class="st">'color'</span>, [<span class="st">'k'</span>])</code></pre>
<p>For the following examples we'll use the same plane as follows:</p>
<pre class="sourceCode python"><code class="sourceCode python">x = np.linspace(-<span class="dv">9</span>, <span class="dv">9</span>, <span class="dv">400</span>)
y = np.linspace(-<span class="dv">5</span>, <span class="dv">5</span>, <span class="dv">400</span>)
x, y = np.meshgrid(x, y)</code></pre>
<p>Take into account that <a target="_blank" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.meshgrid.html"><code>numpy.meshgrid</code></a> creates a grid replicating the first array horizontally and the second array vertically, in the same way of <a target="_blank" href="http://uk.mathworks.com/help/matlab/ref/meshgrid.html">MATLAB's meshgrid</a>. Do not confuse with <a target="_blank" href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.mgrid.html"><code>numpy.mgrid</code></a>, which result will be a transposed meshgrid.</p>
<p>To simplify our examples, let's define a function to plot the origin axes:</p>
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> axes():
plt.axhline(<span class="dv">0</span>, alpha=.<span class="dv">1</span>)
plt.axvline(<span class="dv">0</span>, alpha=.<span class="dv">1</span>)</code></pre>
<h2 id="parabolas">Parabolas</h2>
<p>Parabolas in standard position follow an equation of the form</p>
<p><span class="math">\[
y^2 = 4ax, \quad \text{with}\ a>0.
\]</span></p>
<p>Using <a target="_blank" href="http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.contour"><code>matplotlib.pyplot.contour</code></a> and arranging <span class="math">\(4ax\)</span> to the left hand side we can draw a parabola in standard position as:</p>
<pre class="sourceCode python"><code class="sourceCode python">a = .<span class="dv">3</span>
axes()
plt.contour(x, y, (y**<span class="dv">2</span> - <span class="dv">4</span>*a*x), [<span class="dv">0</span>], colors=<span class="st">'k'</span>)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib parabola" src="https://mmas.github.io/images/figure_1_drawing_conics_matplotlib__parabola.png" /></p>
</div>
<p>The focus is <span class="math">\((a,0)\)</span> and the directrix is <span class="math">\(x=-a\)</span>,</p>
<pre class="sourceCode python"><code class="sourceCode python">axes()
plt.contour(x, y, (y**<span class="dv">2</span> - <span class="dv">4</span>*a*x), [<span class="dv">0</span>], colors=<span class="st">'k'</span>, alpha=.<span class="dv">1</span>)
<span class="co"># Focus.</span>
plt.plot(a, <span class="dv">0</span>, <span class="st">'.'</span>)
<span class="co"># Directrix.</span>
plt.axvline(-a)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib parabola" src="https://mmas.github.io/images/figure_2_drawing_conics_matplotlib__parabola.png" /></p>
</div>
<p>To define a parabola in a non-standard position is has to follow the form</p>
<p><span class="math">\[
Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0, \quad \text{where}\ B^2 - 4AC = 0.
\]</span></p>
<p>A parabola in a non-standard position:</p>
<pre class="sourceCode python"><code class="sourceCode python">a, b, c, d, e, f = <span class="dv">1</span>, -<span class="dv">2</span>, <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">2</span>, -<span class="dv">10</span>
<span class="kw">assert</span> b**<span class="dv">2</span> - <span class="dv">4</span>*a*c == <span class="dv">0</span>
axes()
plt.contour(x, y,(a*x**<span class="dv">2</span> + b*x*y + c*y**<span class="dv">2</span> + d*x + e*y + f), [<span class="dv">0</span>], colors=<span class="st">'k'</span>)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib parabola" src="https://mmas.github.io/images/figure_3_drawing_conics_matplotlib__parabola.png" /></p>
</div>
<h2 id="ellipses-and-circles">Ellipses and circles</h2>
<p>Ellipses in standard position follow an equation of the form</p>
<p><span class="math">\[
{x^2 \over a^2} + {y^2 \over b^2} = 1, \quad \text{where}\ a > b > 0.
\]</span></p>
<p>In the case of the circles, <span class="math">\(a=b\)</span>.</p>
<p>A circle in standard position:</p>
<pre class="sourceCode python"><code class="sourceCode python">a = <span class="dv">2</span>.
b = <span class="dv">2</span>.
axes()
plt.contour(x, y,(x**<span class="dv">2</span>/a**<span class="dv">2</span> + y**<span class="dv">2</span>/b**<span class="dv">2</span>), [<span class="dv">1</span>], colors=<span class="st">'k'</span>)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib circle" src="https://mmas.github.io/images/figure_4_drawing_conics_matplotlib__circle.png" /></p>
</div>
<p>A ellipse in standard position:</p>
<pre class="sourceCode python"><code class="sourceCode python">a = <span class="dv">4</span>.
b = <span class="dv">2</span>.
axes()
plt.contour(x, y,(x**<span class="dv">2</span>/a**<span class="dv">2</span> + y**<span class="dv">2</span>/b**<span class="dv">2</span>), [<span class="dv">1</span>], colors=<span class="st">'k'</span>)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib ellipse" src="https://mmas.github.io/images/figure_5_drawing_conics_matplotlib__ellipse.png" /></p>
</div>
<p>In the case of the parabola the eccentricity <span class="math">\(e=1\)</span>. For ellipses, the eccentricity <span class="math">\(0 < e < 1\)</span> and it is defined as</p>
<p><span class="math">\[
e = \sqrt{1 - {b^2 \over a^2}}.
\]</span></p>
<p>The foci are <span class="math">\((\pm ae,0)\)</span> and the directrices are <span class="math">\(x=\pm {a \over e}\)</span>,</p>
<pre class="sourceCode python"><code class="sourceCode python">axes()
plt.contour(x, y,(x**<span class="dv">2</span>/a**<span class="dv">2</span> + y**<span class="dv">2</span>/b**<span class="dv">2</span>), [<span class="dv">1</span>], colors=<span class="st">'k'</span>, alpha=.<span class="dv">1</span>)
<span class="co"># Eccentricity.</span>
e = np.sqrt(<span class="dv">1</span> - b**<span class="dv">2</span>/a**<span class="dv">2</span>)
<span class="co"># Foci.</span>
plt.plot(a*e, <span class="dv">0</span>, <span class="st">'.'</span>, -a*e, <span class="dv">0</span>, <span class="st">'.'</span>)
<span class="co"># Directrices.</span>
plt.axvline(a/e)
plt.axvline(-a/e)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib ellipse" src="https://mmas.github.io/images/figure_6_drawing_conics_matplotlib__ellipse.png" /></p>
</div>
<p>An ellipsis in a non-standard position has an equation in the same form of the parabola, but with the requisite</p>
<p><span class="math">\[
B^2 - 4AC < 0,
\]</span></p>
<pre class="sourceCode python"><code class="sourceCode python">a, b, c, d, e, f = <span class="dv">4</span>, -<span class="dv">5</span>, <span class="dv">2</span>, <span class="dv">4</span>, -<span class="dv">3</span>, -<span class="dv">3</span>
<span class="kw">assert</span> b**<span class="dv">2</span> - <span class="dv">4</span>*a*c < <span class="dv">0</span>
axes()
plt.contour(x, y,(a*x**<span class="dv">2</span> + b*x*y + c*y**<span class="dv">2</span> + d*x + e*y + f), [<span class="dv">0</span>], colors=<span class="st">'k'</span>)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib ellipse" src="https://mmas.github.io/images/figure_7_drawing_conics_matplotlib__ellipse.png" /></p>
</div>
<h2 id="hyperbolas">Hyperbolas</h2>
<p>Hyperbolas in standard position follow an equation of the form</p>
<p><span class="math">\[
{x^2 \over a^2} - {y^2 \over b^2} = 1, \quad \text{where}\ a, b > 0.
\]</span></p>
<p>An hyperbola in standard position:</p>
<pre class="sourceCode python"><code class="sourceCode python">a = <span class="dv">2</span>.
b = <span class="dv">1</span>.
axes()
plt.contour(x, y,(x**<span class="dv">2</span>/a**<span class="dv">2</span> - y**<span class="dv">2</span>/b**<span class="dv">2</span>), [<span class="dv">1</span>], colors=<span class="st">'k'</span>)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib hyperbola" src="https://mmas.github.io/images/figure_8_drawing_conics_matplotlib__hyperbola.png" /></p>
</div>
<p>The eccentricity <span class="math">\(e > 1\)</span> is defined as</p>
<p><span class="math">\[
e = \sqrt{1 + {b^2 \over a^2}},
\]</span></p>
<p>The foci and directrices are the same as the ellipsis and the asymptotes can be found at <span class="math">\(y = \pm {b \over a}x\)</span>,</p>
<pre class="sourceCode python"><code class="sourceCode python">axes()
plt.contour(x, y,(x**<span class="dv">2</span>/a**<span class="dv">2</span> - y**<span class="dv">2</span>/b**<span class="dv">2</span>), [<span class="dv">1</span>], colors=<span class="st">'k'</span>, alpha=.<span class="dv">1</span>)
<span class="co"># Eccentricity.</span>
e = np.sqrt(<span class="dv">1</span> + b**<span class="dv">2</span>/a**<span class="dv">2</span>)
<span class="co"># Foci.</span>
plt.plot(a*e, <span class="dv">0</span>, <span class="st">'.'</span>, -a*e, <span class="dv">0</span>, <span class="st">'.'</span>)
<span class="co"># Directrices.</span>
plt.axvline(a/e)
plt.axvline(-a/e)
<span class="co"># Asymptotes.</span>
plt.plot(x[<span class="dv">0</span>,:], b/a*x[<span class="dv">0</span>,:], <span class="st">'--'</span>)
plt.plot(x[<span class="dv">0</span>,:], -b/a*x[<span class="dv">0</span>,:], <span class="st">'--'</span>)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib hyperbola" src="https://mmas.github.io/images/figure_9_drawing_conics_matplotlib__hyperbola.png" /></p>
</div>
<p>An hyperbola in a non-standard position has an equation in the same form as before with the requisite</p>
<p><span class="math">\[
B^2 - 4AC > 0,
\]</span></p>
<pre class="sourceCode python"><code class="sourceCode python">a, b, c, d, e, f = <span class="dv">1</span>, -<span class="dv">3</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>
<span class="kw">assert</span> b**<span class="dv">2</span> - <span class="dv">4</span>*a*c > <span class="dv">0</span>
axes()
plt.contour(x, y,(a*x**<span class="dv">2</span> + b*x*y + c*y**<span class="dv">2</span> + d*x + e*y + f), [<span class="dv">0</span>], colors=<span class="st">'k'</span>)
plt.show()</code></pre>
<div class="figure">
<p><img alt="Python Matplotlib hyperbola" src="https://mmas.github.io/images/figure_10_drawing_conics_matplotlib__hyperbola.png" /></p>
</div>
</section>
</article>
</section>
<footer></footer>
<script type="text/javascript" src="/js/article.js"></script>
</body>
</html>