用pyplot画一个圆 [英] plot a circle with pyplot

查看:183
本文介绍了用pyplot画一个圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令人惊讶的是,我没有找到关于如何使用matplotlib.pyplot(请不要使用pylab)绘制圆作为输入中心(x,y)和半径r的简单描述.我尝试了一些变体:

surprisingly I didn't find a straight-forward description on how to draw a circle with matplotlib.pyplot (please no pylab) taking as input center (x,y) and radius r. I tried some variants of this:

import matplotlib.pyplot as plt
circle=plt.Circle((0,0),2)
# here must be something like circle.plot() or not?
plt.show()

...但是仍然无法正常工作.

... but still didn't get it working.

推荐答案

您需要将其添加到轴中. CircleArtist的子类,而axes具有add_artist方法.

You need to add it to an axes. A Circle is a subclass of an Artist, and an axes has an add_artist method.

这是执行此操作的示例:

Here's an example of doing this:

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')

这将导致下图:

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

The first circle is at the origin, but by default clip_on is True, so the circle is clipped when ever it extends beyond the axes. The third (green) circle shows what happens when you don't clip the Artist. It extends beyond the axes (but not beyond the figure, ie the figure size is not automatically adjusted to plot all of your artists).

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

The units for x, y and radius correspond to data units by default. In this case, I didn't plot anything on my axes (fig.gca() returns the current axes), and since the limits have never been set, they defaults to an x and y range from 0 to 1.

以下是该示例的续篇,展示了单位的重要性:

Here's a continuation of the example, showing how units matter:

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')

其结果是:

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

You can see how I set the fill of the 2nd circle to False, which is useful for encircling key results (like my yellow data point).

这篇关于用pyplot画一个圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆