-1

I can draw a circle by scatter, which has been shown in the image. But I want to draw them buy a line, because there are many circles in total, I need to link nodes together for a certain circle. Thanks. enter image description here

Aaron
  • 45
  • 1
  • 7

3 Answers3

2

I the order of the points is random, you can change X-Y to polar, and sort the data by angle:

create some random order points first:

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]

Then use arctan2() to calculate the angle, and sort the data by it:

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)

here is the output:

enter image description here

HYRY
  • 94,853
  • 25
  • 187
  • 187
0

Here is one way to do it. This answer uses different methods than the linked possible duplicate, so may be worth keeping.

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)

If all you have are the x and y points, you can use PathPatch. Here's a tutorial

Community
  • 1
  • 1
Gabriel
  • 10,524
  • 1
  • 23
  • 28
  • my data is the coordinate of the dots on a edge of a circle, or like a circle. Not exactly circles. Maybe what I need is 'closed curves'. Not circle. – Aaron Aug 01 '14 at 04:31
  • I feel path is not applicable. Coz I need to give out all the vertex and the numbering. That is too much job. – Aaron Aug 01 '14 at 04:34
  • 1
    do you want to just plot them? how are the points ordered in your arrays. if they go in order around the circle just use `plt.plot( x,y )` – Gabriel Aug 01 '14 at 04:46
  • if they are not ordered you could sort the left half in increasing y and the right have in decreasing y to get them in the right order – Gabriel Aug 01 '14 at 05:13
0

If your data points are already in order, the plot command should work fine. If you're looking to generate a circle from scratch, you can use a parametric equation.

>>> 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)
Ben
  • 6,986
  • 6
  • 44
  • 71