2
votes

I am trying to circular contour lines around an array of random values of radius. The result should be a bunch of concentric circles with different radius. However I am not too sure how to plot the theta so that for each radius, all values of theta is plotted to form a line.

import random
import numpy as np 
r= sort(np.array([ random.random()*5 for i in arange(100) ]))
len(r)
theta = [t for t in linspace(0,2*pi,100)]
ax = plt.subplot(111, polar=True)
ax.plot(theta, r, 'o',color='r', linewidth=3)
ax.set_rmax(2.0)
ax.grid(True)

enter image description here

Thank you.

2

2 Answers

1
votes

Here is a one-line addition that I think does what you want:

import random
import numpy as np
import matplotlib.pyplot as plt
r= np.sort(np.array([ random.random()*5 for i in np.arange(100) ]))
len(r)
theta = [t for t in np.linspace(0,2*np.pi,100)]
ax = plt.subplot(111, polar=True)
ax.plot(theta, r, 'o',color='r', linewidth=3)
ax.set_rmax(2.0)
ax.grid(True)

[ax.plot(theta, rcirc*np.ones(100)) for rcirc in r.max()*np.random.rand(5)]

plt.show()
1
votes

A quick-and-dirty way to do it would be to use np.linspace to effectively draw a polygon (as I think you were attempting to do):

import numpy as np
from matplotlib import pyplot as plt

# some random radii
r = np.random.rand(10)

# 1000 angles linearly spaced between 0 and 2pi
t = np.linspace(0, 2 * np.pi, 1000)

# broadcast r against t to make each a (1000, 10) array
r, t = np.broadcast_arrays(r[None, :], t[:, None])

# plot the lines
fig, ax = plt.subplots(1, 1, subplot_kw={'polar':True})
ax.plot(t, r, '-')

enter image description here

I'm sure there must be a more elegant way to do this, though.