gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:58:12 26 4
gpt4 key购买 nike

我真的搞不懂,我需要在图像上叠加一个椭圆。问题是用 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 - 没有 fillcolor 无法在 python 中绘制椭圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34723519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com