matplotlib 如何绘制椭圆[重复]

bksxznpy  于 2023-10-24  发布在  其他
关注(0)|答案(1)|浏览(99)

此问题已在此处有答案

Plot Ellipse with matplotlib.pyplot(3个答案)
上个月关门了。
我可以像这样绘制椭圆:

from matplotlib.patches import Ellipse
import matplotlib as mpl
%matplotlib inline
from matplotlib import pyplot as plt

mean = [ 19.92977907 ,  5.07380955]
width = 30
height = 1.01828848
angle = -54
ell = mpl.patches.Ellipse(xy=mean, width=width, height=height, angle = 180+angle)
fig, ax = plt.subplots()
ax.add_artist(ell)

ax.set_aspect('equal')
ax.set_xlim(-100, 100)
ax.set_ylim(-100, 100)
plt.show()

但是,这需要我手动设置轴数据限制。它可以自动设置吗?我的意思是,如何摆脱ax.set_xlim(-100, 100)ax.set_ylim(-100, 100)?或者,什么是绘制椭圆的好方法?

wn9m85ua

wn9m85ua1#

您需要使用add_patch添加patch,而不是add_artist,然后数据限制将使用ax.autoscale正确更新:

from matplotlib.patches import Ellipse
import matplotlib as mpl
%matplotlib inline
from matplotlib import pyplot as plt

mean = [ 19.92977907 ,  5.07380955]
width = 30
height = 1.01828848
angle = -54
ell = mpl.patches.Ellipse(xy=mean, width=width, height=height, angle = 180+angle)
fig, ax = plt.subplots()

ax.add_patch(ell)
ax.set_aspect('equal')
ax.autoscale()
plt.show()

相关问题