Python 和 Matplotlib : how to plot ellipse?

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

最佳答案

您需要使用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()

enter image description here

关于Python 和 Matplotlib : how to plot ellipse?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32371996/

相关文章:

Python - 合并文本文件(特定行)

python - 在 Python 中实现线性回归

python - 填补 numpy 数组中的空白

python - 如何从单独的进程(例如,编辑器、vim)在 Jupyter Notebook 服务器中创建和执行单元格?

python - 将整数列表转换为字符串,在 Python 中的 x 元素后仅用一个逗号

python - 如何高效地将C/C++逻辑转换成python?

python - 卸载旧版本的 python (fedora)

python - 类对象不更新空列表

python - 如何仅用 3 个已知点制作连续的抛物线弧?

python - matplotlib.pyplot.legend() 出现类型错误 : zip argument #2 must support iteration,