如何用matplotlib + python数据绘制圆圈?

时间:2014-08-01 03:35:52

标签: python matplotlib

我可以通过scatter绘制一个圆圈,该圆圈已在图片中显示。但我想画他们买一条线,因为总共有很多圈子,我需要将节点链接在一起一定的圈子。谢谢。 enter image description here

3 个答案:

答案 0 :(得分:1)

我的点的顺序是随机的,您可以将X-Y更改为极性,并按角度对数据进行排序:

首先创建一些随机订单点:

import pylab as pl
import numpy as np

angle = np.arange(0, np.pi*2, 0.05)
r = 50 + np.random.normal(0, 2, angle.shape)

x = r * np.cos(angle)
y = r * np.sin(angle)

idx = np.random.permutation(angle.shape[0])
x = x[idx]
y = y[idx]

然后使用arctan2()计算角度,并按以下方式对数据进行排序:

angle = np.arctan2(x, y)
order = np.argsort(angle)
x = x[order]
y = y[order]

fig, ax = pl.subplots()
ax.set_aspect(1.0)
x2 = np.r_[x, x[0]]
y2 = np.r_[y, y[0]]
ax.plot(x, y, "o")
ax.plot(x2, y2, "r", lw=2)

这是输出:

enter image description here

答案 1 :(得分:0)

这是一种方法。此答案使用的方法与链接的possible duplicate不同,因此可能值得保留。

import matplotlib.pyplot as plt
from matplotlib import patches

fig = plt.figure(figsize=plt.figaspect(1.0))
ax = fig.add_subplot(111)

cen = (2.0,1.0); r = 3.0
circle = patches.Circle(cen, r, facecolor='none')
ax.add_patch(circle)
ax.set_xlim(-6.0, 6.0)
ax.set_ylim(-6.0, 6.0)

如果您拥有的是xy点,则可以使用PathPatch。这是一个tutorial

答案 2 :(得分:0)

如果您的数据点已经有序,plot命令应该可以正常工作。如果您想从头开始生成圆,可以使用参数方程。

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> t = np.linspace(0,2*np.pi, 100)
>>> x = np.cos(t)
>>> y = np.sin(t)
>>> plt.plot(x,y)
相关问题