我可以像这样绘制椭圆:
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) ? 或者,绘制椭圆的好方法是什么?
最佳答案
您需要使用add_patch
而不是add_artist
添加patch
,然后数据限制将使用ax正确更新。自动缩放
:
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()
关于Python 和 Matplotlib : how to plot ellipse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32371996/