令人惊讶的是,我没有找到一个简单明了的描述如何使用matplotlib.pyplot(请勿使用pylab)以中心点(x,y)和半径r作为输入绘制圆形的方法。我尝试了一些变体:
import matplotlib.pyplot as plt
circle=plt.Circle((0,0),2)
# here must be something like circle.plot() or not?
plt.show()
...但仍然没有使其工作。
令人惊讶的是,我没有找到一个简单明了的描述如何使用matplotlib.pyplot(请勿使用pylab)以中心点(x,y)和半径r作为输入绘制圆形的方法。我尝试了一些变体:
import matplotlib.pyplot as plt
circle=plt.Circle((0,0),2)
# here must be something like circle.plot() or not?
plt.show()
...但仍然没有使其工作。
您需要将其添加到一个坐标轴(axes)上。一个Circle
是Patch
的一个子类,而一个axes
具有一个add_patch
方法。(您还可以使用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_patch(circle1)
ax.add_patch(circle2)
ax.add_patch(circle3)
fig.savefig('plotcircles.png')
以下是该代码的结果:
第一个圆在原点,但默认情况下clip_on
为True
,当圆超出axes
范围时会被裁剪。第三个(绿色)圆显示了在不裁剪Artist
的情况下会发生什么。它超出了轴的范围(但没有超出图片大小,即图片大小并没有自动调整以绘制您所有的艺术家)。
x、y和半径的单位默认情况下对应于数据单位。在这种情况下,我没有在我的轴上绘制任何东西(fig.gca()
返回当前轴),并且由于限制从未被设置,它们默认为从0到1的x和y范围。
以下是示例的继续,展示了单位的影响:
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_patch(circle1)
ax.add_patch(circle2)
ax.add_patch(circle3)
fig.savefig('plotcircles2.png')
这导致:
你可以看到我将第二个圆的填充设置为False
,这对于围绕关键结果(例如我的黄色数据点)非常有用。
matplotlib.pyplot.Circle == matplotlib.patches.Circle
的结果为 True
,因此它们很可能是别名。 - Evgeni Sergeevimport matplotlib.pyplot as plt
circle1 = plt.Circle((0, 0), 0.2, color='r')
plt.gca().add_patch(circle1)
快速简明版本的被接受答案,以快速将圆形插入现有图中。请参考被接受的答案和其他答案了解详情。
顺便说一下:
gca()
意思是获取当前轴dir(fig)
向我展示了超过30个“get”方法,但是gca
没有“get_current_axis”的别名。这种FYI(供参考)的回答非常棒。 - uhohplt.gca()
д»Јж›їplt.gcf().gca()
гЂ‚ - Andre Holznercircles
的函数。circles
类似于scatter
,但所绘制圆的尺寸是以数据单位表示的。from pylab import *
figure(figsize=(8,8))
ax=subplot(aspect='equal')
#plot one circle (the biggest one on bottom-right)
circles(1, 0, 0.5, 'r', alpha=0.2, lw=5, edgecolor='b', transform=ax.transAxes)
#plot a set of circles (circles in diagonal)
a=arange(11)
out = circles(a, a, a*0.2, c=a, alpha=0.5, edgecolor='none')
colorbar(out)
xlim(0,10)
ylim(0,10)
transform=ax.transAxes
是什么意思? - wsdzbmmplleaflet
一起使用吗?如果可以的话,能否提供一个例子? - François M.matplotlib
的,我猜它应该与mplleaflet
兼容,尽管我从未尝试过。 - Syrtis Major#!/usr/bin/python
import matplotlib.pyplot as plt
import numpy as np
def xy(r,phi):
return r*np.cos(phi), r*np.sin(phi)
fig = plt.figure()
ax = fig.add_subplot(111,aspect='equal')
phis=np.arange(0,6.28,0.01)
r =1.
ax.plot( *xy(r,phis), c='r',ls='-' )
plt.show()
或者,如果您喜欢的话,可以查看 path
(路径),http://matplotlib.sourceforge.net/users/path_tutorial.html
如果您希望“圆形”保持视觉纵横比为1,无论数据坐标如何,您可以使用scatter()方法。http://matplotlib.org/1.3.1/api/pyplot_api.html#matplotlib.pyplot.scatter
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
r = [100, 80, 60, 40, 20] # in points, not data units
fig, ax = plt.subplots(1, 1)
ax.scatter(x, y, s=r)
fig.show()
扩展常见用例的接受答案。特别是:
以自然宽高比查看圆。
自动扩展轴限制以包括新绘制的圆。
自包含示例:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.add_patch(plt.Circle((0, 0), 0.2, color='r', alpha=0.5))
ax.add_patch(plt.Circle((1, 1), 0.5, color='#00ffff', alpha=0.5))
ax.add_artist(plt.Circle((1, 0), 0.5, color='#000033', alpha=0.5))
#Use adjustable='box-forced' to make the plot area square-shaped as well.
ax.set_aspect('equal', adjustable='datalim')
ax.plot() #Causes an autoscale update.
plt.show()
注意 ax.add_patch(..)
和 ax.add_artist(..)
之间的区别:两者中只有前者考虑了自动缩放机制(参考:讨论),因此在运行上述代码后,我们得到:
另请参阅:set_aspect(..)
文档。
fig, ax = plt.subplots()
,否则你会得到两个窗口(其中一个是空白的)。 - albus_c我看到使用 (.circle) 绘制的图表,但根据你想要做什么,你也可以尝试这个:
import matplotlib.pyplot as plt
import numpy as np
x = list(range(1,6))
y = list(range(10, 20, 2))
print(x, y)
for i, data in enumerate(zip(x,y)):
j, k = data
plt.scatter(j,k, marker = "o", s = ((i+1)**4)*50, alpha = 0.3)
centers = np.array([[5,18], [3,14], [7,6]])
m, n = make_blobs(n_samples=20, centers=[[5,18], [3,14], [7,6]], n_features=2,
cluster_std = 0.4)
colors = ['g', 'b', 'r', 'm']
plt.figure(num=None, figsize=(7,6), facecolor='w', edgecolor='k')
plt.scatter(m[:,0], m[:,1])
for i in range(len(centers)):
plt.scatter(centers[i,0], centers[i,1], color = colors[i], marker = 'o', s = 13000, alpha = 0.2)
plt.scatter(centers[i,0], centers[i,1], color = 'k', marker = 'x', s = 50)
plt.savefig('plot.png')
make_blobs
? - iacopomake_blobs
函数:https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_blobs.html - Arun Dasmarkersize
参数可以调整圆的半径:import matplotlib.pyplot as plt
plt.plot(200, 2, 'o', markersize=7)
你好,我已经编写了一个绘制圆形的代码。 它可以帮助绘制各种类型的圆形。 图像显示半径为1,中心在0,0的圆 中心和半径可以根据需要进行编辑。
## Draw a circle with center and radius defined
## Also enable the coordinate axes
import matplotlib.pyplot as plt
import numpy as np
# Define limits of coordinate system
x1 = -1.5
x2 = 1.5
y1 = -1.5
y2 = 1.5
circle1 = plt.Circle((0,0),1, color = 'k', fill = False, clip_on = False)
fig, ax = plt.subplots()
ax.add_artist(circle1)
plt.axis("equal")
ax.spines['left'].set_position('zero')
ax.spines['bottom'].set_position('zero')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
plt.xlim(left=x1)
plt.xlim(right=x2)
plt.ylim(bottom=y1)
plt.ylim(top=y2)
plt.axhline(linewidth=2, color='k')
plt.axvline(linewidth=2, color='k')
##plt.grid(True)
plt.grid(color='k', linestyle='-.', linewidth=0.5)
plt.show()
祝你好运
plt.Circle(..)
指向matplotlib.patches.Circle()
。因此,一个没有pyplot的解决方案是circle = matplotlib.patches.Circle(..); axes.add_artist(circle)
。 - ImportanceOfBeingErnest