python circle函数如何画圆_用pyplot画一个圆

您需要将其添加到轴。A Circle是的子类Artist,并且axes具有add_artist方法。

这是执行此操作的示例:

import matplotlib.pyplot as plt

circle1 = plt.Circle((0, 0), 0.2, color='r')

circle2 = plt.Circle((0.5, 0.5), 0.2, color='blue')

circle3 = plt.Circle((1, 1), 0.2, color='g', clip_on=False)

fig, ax = plt.subplots() # note we must use plt.subplots, not plt.subplot

# (or if you have an existing figure)

# fig = plt.gcf()

# ax = fig.gca()

ax.add_artist(circle1)

ax.add_artist(circle2)

ax.add_artist(circle3)

fig.savefig('plotcircles.png')

结果如下图:

5db2699400011b4605000375.jpg

第一个圆是原点,但默认情况下clip_on是True,因此,只要圆超出,就会对其进行裁剪axes。第三个(绿色)圆圈显示了不剪切时会发生的情况Artist。它超出了轴(但不超出图形,即图形大小不会自动调整以绘制所有艺术家)。

默认情况下,x,y和半径的单位对应于数据单位。在这种情况下,我没有在轴上绘制任何内容(fig.gca()返回当前轴),并且由于从未设置极限,因此它们的默认x和y范围为0到1。

这是该示例的继续,显示了单位的重要性:

circle1 = plt.Circle((0, 0), 2, color='r')

# now make a circle with no fill, which is good for hi-lighting key results

circle2 = plt.Circle((5, 5), 0.5, color='b', fill=False)

circle3 = plt.Circle((10, 10), 2, color='g', clip_on=False)

ax = plt.gca()

ax.cla() # clear things for fresh plot

# change default range so that new circles will work

ax.set_xlim((0, 10))

ax.set_ylim((0, 10))

# some data

ax.plot(range(11), 'o', color='black')

# key data point that we are encircling

ax.plot((5), (5), 'o', color='y')

ax.add_artist(circle1)

ax.add_artist(circle2)

ax.add_artist(circle3)

fig.savefig('plotcircles2.png')

结果是:

5db2699e00012b0805000375.jpg

您会看到如何将第二个圆的填充设置为False,这对于环绕关键结果(例如我的黄色数据点)很有用。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值