简体   繁体   English

使用 matplotlib.pyplot (Python) 绘制椭圆

[英]Plot Ellipse with matplotlib.pyplot (Python)

Sorry if this is a stupid question, but is there an easy way to plot an ellipse with matplotlib.pyplot in Python?对不起,如果这是一个愚蠢的问题,但是有没有一种简单的方法可以在 Python 中使用matplotlib.pyplot绘制椭圆? I was hoping there would be something similar to matplotlib.pyplot.arrow , but I can't find anything.我希望会有类似于matplotlib.pyplot.arrow东西,但我找不到任何东西。

Is the only way to do it using matplotlib.patches with draw_artist or something similar?使用matplotlib.patchesdraw_artist或类似的东西是唯一的方法吗? I would hope that there is a simpler method, but the documentation doesn't offer much help.我希望有一种更简单的方法,但文档并没有提供太多帮助。

The matplotlib ellipse demo is nice. matplotlib 椭圆演示很好。 But I could not implement it in my code without a for loop.但是我无法在没有 for 循环的情况下在我的代码中实现它。 I was getting an axes figure error.我收到了一个轴图错误。 Here is what I did instead, where of course the xy center are my own coordinates with respective width and height based on the image over which I plotted the ellipse.这是我所做的,当然,xy 中心是我自己的坐标,具有各自的宽度和高度,基于我在其上绘制椭圆的图像。

from matplotlib.patches import Ellipse

plt.figure()
ax = plt.gca()

ellipse = Ellipse(xy=(157.18, 68.4705), width=0.036, height=0.012, 
                        edgecolor='r', fc='None', lw=2)
ax.add_patch(ellipse)

This code is based partially on the very first code box on this page .此代码部分基于此页面上的第一个代码框。 See Chris's response above for a link to matplotlib.patches.Ellipse .有关matplotlib.patches.Ellipse的链接,请参阅上面 Chris 的回复。

If you do not want to use a patch, you can use the parametric equation of an ellipse:如果不想使用补丁,可以使用椭圆的参数方程:

x = u + a cos(t) ; x = u + a cos(t) ; y = v + b sin(t) y = v + b sin(t)

import numpy as np
from matplotlib import pyplot as plt
from math import pi

u=1.     #x-position of the center
v=0.5    #y-position of the center
a=2.     #radius on the x-axis
b=1.5    #radius on the y-axis

t = np.linspace(0, 2*pi, 100)
plt.plot( u+a*np.cos(t) , v+b*np.sin(t) )
plt.grid(color='lightgray',linestyle='--')
plt.show()

Which gives:这使: 带参数方程的 x 向椭圆

The ellipse can be rotated thanks to a 2D-rotation matrix :由于二维旋转矩阵,椭圆可以旋转:

import numpy as np
from matplotlib import pyplot as plt
from math import pi, cos, sin

u=1.       #x-position of the center
v=0.5      #y-position of the center
a=2.       #radius on the x-axis
b=1.5      #radius on the y-axis
t_rot=pi/4 #rotation angle

t = np.linspace(0, 2*pi, 100)
Ell = np.array([a*np.cos(t) , b*np.sin(t)])  
     #u,v removed to keep the same center location
R_rot = np.array([[cos(t_rot) , -sin(t_rot)],[sin(t_rot) , cos(t_rot)]])  
     #2-D rotation matrix

Ell_rot = np.zeros((2,Ell.shape[1]))
for i in range(Ell.shape[1]):
    Ell_rot[:,i] = np.dot(R_rot,Ell[:,i])

plt.plot( u+Ell[0,:] , v+Ell[1,:] )     #initial ellipse
plt.plot( u+Ell_rot[0,:] , v+Ell_rot[1,:],'darkorange' )    #rotated ellipse
plt.grid(color='lightgray',linestyle='--')
plt.show()

Returns:返回: 带参数方程的旋转椭圆

Have you seen the matplotlib ellipse demo ?你看过matplotlib 椭圆演示吗? Here they use matplotlib.patches.Ellipse .在这里,他们使用matplotlib.patches.Ellipse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM