编程语言
首页 > 编程语言> > 没有fillcolor,无法在python中绘制椭圆

没有fillcolor,无法在python中绘制椭圆

作者:互联网

我真的无法得到这个,我需要在图像上叠加一个椭圆.问题是使用matplotlib.patches绘制椭圆,但最后是白色图形或填充椭圆.代码很原始,我不知道该怎么做,因为我从来没有使用matplotlib库的这一部分.

import matplotlib.pyplot as plt
import numpy.random as rnd
from matplotlib.patches import Ellipse


ells = [Ellipse(xy=[0,0], width=30, height=10, angle=0,edgecolor='b', lw=4)]

fig = plt.figure(0)
ax = fig.add_subplot(111, aspect='equal')
for e in ells:
    ax.add_artist(e)
    e.set_alpha(1)
    e.set_facecolor(none) #obvious mistake, but no idea what to do

ax.set_xlim(-100, 100)
ax.set_ylim(-100, 100)

plt.show()

我试着查找matplotlib.patches页面,但没有关于如何绘制椭圆的明确信息,只是正在使用的函数.

非常感谢一些帮助.

感谢您的时间.

解决方法:

通常,要在matplotlib中为参数指定无颜色,请使用字符串’none’.这同样适用于边缘颜色,面部颜色等.

在你的情况下:

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

fig, ax = plt.subplots()

ax.axis('equal')
ell = Ellipse(xy=[0,0], width=30, height=10, angle=0,
              edgecolor='b', lw=4, facecolor='none')

ax.add_artist(ell)
ax.set(xlim=[-100, 100], ylim=[-100, 100])

plt.show()

enter image description here

具体来说,您也可以使用fill = False,如注释中的@ M4rtini注释.在matplotlib中,使用set(foo = bar)或set_foo(bar)方法通常与传入kwarg foo = bar相同.例如:

import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

fig, ax = plt.subplots()

ax.axis('equal')
ell = Ellipse(xy=[0,0], width=30, height=10, angle=0,
              edgecolor='b', lw=4, fill=False)

ax.add_artist(ell)
ax.set(xlim=[-100, 100], ylim=[-100, 100])

plt.show()

标签:python,matplotlib,plot,ellipse
来源: https://codeday.me/bug/20190523/1156703.html