gpt4 book ai didi

python - Python 的 Matplotlib 模块的奇怪行为 - 绘制圆

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:34 25 4
gpt4 key购买 nike

我目前正在编写一个程序,该程序必须生成一组图。每个图必须有 3 个同心圆,其半径由数据集确定。此外,还必须添加另一个可以具有不同中心的红色圆圈。但是,我遇到了各种问题。除非圆的半径太大,否则我应该在图上看到 3 个黑色圆圈和 1 个红色圆圈,但我没有。

我分离了制作情节的代码片段,它在这里 -

import matplotlib.pyplot as plt

fig1 = plt.figure(1, figsize=(6,6))
plt.xlim(-30,30)
plt.ylim(-30,30)

rcircle1 = plt.Circle( (0,0), 6.0, edgecolor="black", facecolor="white")
rcircle2 = plt.Circle( (0,0), 12.0, edgecolor="black", facecolor="white")
rcircle3 = plt.Circle( (0,0), 18.0, edgecolor="black", facecolor="white")
bcircle = plt.Circle( (8.5,-5.8) ,2, edgecolor="red", facecolor="white")

ax = fig1.gca()
ax.add_artist(rcircle1)
ax.add_artist(rcircle2)
ax.add_artist(rcircle3)
ax.add_artist(bcircle)

fig1.savefig("Model.png", dpi=150)

上面的输出是-

Output of above code.

我尝试查看与 Circle()add_artist() 关联的各种类变量,但无法找到可能影响此行为的内容。

我目前的解决方法是以下代码 -

import numpy as np
import matplotlib.pyplot as plt

th = np.arange(-3.14,3.14,0.01)

fig1 = plt.figure(1,figsize=(6,6))
plt.xlim(-30,30)
plt.ylim(-30,30)

plt.plot( 6*np.cos(th), 6*np.sin(th), color="black")
plt.plot( 12*np.cos(th), 12*np.sin(th), color="black")
plt.plot( 18*np.cos(th), 18*np.sin(th), color="black")
# (8,5, -5,8)
plt.plot( 2*np.cos(th) + 8.5, 2*np.sin(th) - 5.8, color="red")

fig1.savefig("Hard.png", dpi=150)

上面代码生成的输出正是我想要的!

enter image description here

虽然这确实有效,但它违背了在 matplotlib 中使用类似 Circle() 的方法的目的。谁能评论为什么第一个代码没有像我预期的那样工作?

最佳答案

您的问题是 facecolor 参数。您最后添加了最大的圆圈,它有一个不透明的中心。

在第二个示例中,您绘制的是一条线,而不是“实心”圆。

要么更改添加圆圈的顺序(或提供 zorder kwarg),要么传入 facecolor='none'(注意:它是字符串 "none",而不是对象 None) 以获得“未填充”的圆圈。

关于python - Python 的 Matplotlib 模块的奇怪行为 - 绘制圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16481224/

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