我可以像这样绘制椭圆:
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)
?
或者,绘制椭圆的好方法是什么?
答案 0 :(得分:2)
您需要使用patch
而不是add_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()