使用matplotlib.pyplot(Python)打印椭圆
如果这是一个愚蠢的问题,很抱歉,但是有没有一种简单的方法可以在Python中使用使用matplotlib.pyplot(Python)打印椭圆,python,matplotlib,ellipse,Python,Matplotlib,Ellipse,如果这是一个愚蠢的问题,很抱歉,但是有没有一种简单的方法可以在Python中使用matplotlib.pyplot绘制椭圆?我希望会有类似于matplotlib.pyplot.arrow的东西,但我找不到任何东西 使用matplotlib.patches和draw\u artist或类似工具是唯一的方法吗?我希望有一个更简单的方法,但文档并没有提供太多帮助。您看到了吗?在这里他们使用。matplotlib椭圆演示很好。但如果没有for循环,我无法在代码中实现它。我得到了一个轴的数字错误。这里是我
matplotlib.pyplot
绘制椭圆?我希望会有类似于matplotlib.pyplot.arrow的东西,但我找不到任何东西
使用
matplotlib.patches
和draw\u artist
或类似工具是唯一的方法吗?我希望有一个更简单的方法,但文档并没有提供太多帮助。您看到了吗?在这里他们使用。matplotlib椭圆演示很好。但如果没有for循环,我无法在代码中实现它。我得到了一个轴的数字错误。这里是我所做的,当然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)
此代码部分基于上的第一个代码框。参见上面Chris的回复,以获取指向
matplotlib.patches.Ellipse
的链接。如果不想使用面片,可以使用椭圆的参数方程:
x=u+a cos(t);y=v+b sin(t)
其中:
借助二维旋转矩阵,椭圆可以旋转:
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()
返回:
我希望能找到更接近标准绘图方法的方法,但我将在下一步对此进行研究。谢谢刚刚注意到您在matplotlib.pyplot中查找某些内容。对不起,我一开始没注意到。搜索
matplotlib.pyplot
API文档没有发现任何信息,因此我担心您将不得不使用matplotlib.patches.eliple
谢谢,这似乎是我必须要做的。我本来希望pyplot包含一些基本的形状打印功能,但我想不可能什么都有@Casper如果你知道椭圆的原点和长/短轴,你可以简单地在轨迹上生成点。根本不需要补丁。在仔细查看matplotlib.patches之后,我意识到这是最简单的解决方案。演示非常有用。这对于快速绘图特别有用(当您更喜欢使用plt.plot/scatter等,而不是创建子绘图的轴时)。请注意,除非设置xlim/ylim,否则很难找到演示椭圆;)完全同意。对于如此简单的东西,这不是一个合理的抽象层次。
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()