在Python中根据x、y、r向量绘制多个圆圈

0 投票
2 回答
4484 浏览
提问于 2025-04-17 14:43

x和y是圆心的位置,r是圆的半径,都是向量。我想一次性把它们全部画出来。类似于:

import matplotlib.pyplot as plt
from matplotlib.patches Circle

#define x,y,r vectors

fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
plt.Circle((x,y),r,color='r')
plt.show()

谢谢。

2 个回答

1

plt.scatter 这个函数可以让你设置绘制点的半径。

根据文档内容

matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o')
[...]

s:
    size in points^2. It is a scalar or an array of the same length as x and y.

通过调整 facecoloredgecolor,你应该能得到想要的效果。

你可以在这里找到一个例子:如何为matplot散点图中的每个气泡设置_gid()?

0

我对Circles补丁不太了解,不过我可以告诉你怎么用标准的绘图命令来实现:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([0.2,0.4])
y = np.array([0.2,1.2])
r = np.array([0.5,0.3])

phi = np.linspace(0.0,2*np.pi,100)

na=np.newaxis

# the first axis of these arrays varies the angle, 
# the second varies the circles
x_line = x[na,:]+r[na,:]*np.sin(phi[:,na])
y_line = y[na,:]+r[na,:]*np.cos(phi[:,na])

plt.plot(x_line,y_line,'-')
plt.show()

基本的思路是给plt.plot(...)命令传入两个二维数组。在这种情况下,它们会被当作一系列的图形来处理。特别是当你要绘制很多图形(也就是很多圆圈)的时候,这种方法比一个一个地绘制圆圈要快得多。

撰写回答