令人惊讶的是,我没有找到如何用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()
…但还是没能用上。
当前回答
如果你想绘制一组圆圈,你可能想看看这篇文章或这个要点(更新一点)。该帖子提供了一个名为circles的功能。
函数圆的工作原理类似于散点,但绘制的圆的大小以数据单位表示。
这里有一个例子:
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)
其他回答
#!/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()
或者,如果您愿意,可以查看路径http://matplotlib.sourceforge.net/users/path_tutorial.html
如果你想绘制一组圆圈,你可能想看看这篇文章或这个要点(更新一点)。该帖子提供了一个名为circles的功能。
函数圆的工作原理类似于散点,但绘制的圆的大小以数据单位表示。
这里有一个例子:
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)
你好,我写了一个画圆的代码。 这对画各种圆都有帮助。 图像显示了半径为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()
祝你好运
我看到使用(.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')
扩展一个通用用例的可接受答案。特别是:
以自然长宽比查看圆形。 自动扩展轴的限制,以包括新绘制的圆。
独立的例子:
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(..)文档。