Python& Matplotlib:如何绘制椭圆?

时间:2015-09-03 09:35:36

标签: python matplotlib

我可以像这样绘制椭圆:

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)? 或者,绘制椭圆的好方法是什么?

1 个答案:

答案 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()

enter image description here

相关问题