Reputation: 31
I am having some trouble with a polar chart I am working on. The figure I should get is an eight-shape (some friends of mine plotted the data in Origin and Excel and it does work), but it looks like the code is not properly written. By looking at the figure, I see that the code is not taking into account the angles I am writing (theta array), but I don't know why it happens. I've already tried some more codes and writing the angles in radians, but nothing seems to work.
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
import matplotlib.pyplot as plt
from pylab import *
import numpy as np
r = np.array([11.7,12.1,10.1,6.6,3.1,1.5,2.3,5.2,
8.7,11.5,12,10.1,6.6,3.3,1.5,2.3,5.3,9.2,11.9])
theta =np.array([0,20,40,60,80,100,120,140,160,180,
200,220,240,260,280,300,320,340,360])
ax = plt.subplot(111, projection='polar')
ax.plot(theta,r)
ax.set_rmax(13)
ax.set_rticks([2,4,6,8,10,12]) # less radial ticks
ax.set_rlabel_position(-40) # get radial labels away from plotted line
ax.grid(True)
ax.set_title("A line plot on a polar axis", va='bottom')
plt.show()
I've also tried this:
r3 = np.array([11.7,12.1,10.1,6.6,3.1,1.5,2.3,5.2,
8.7,11.5,12,10.1,6.6,3.3,1.5,2.3,5.3,9.2,11.9])
theta3 =np.array([0,20,40,60,80,100,120,140,160,180,
200,220,240,260,280,300,320,340,360])
fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
c = ax.scatter(theta3, r3)
Upvotes: 3
Views: 285
Reputation: 7186
Counter-intuitively, while the polar plot shows its theta axis in degrees, it actually expects the coordinates to be in radians:
import numpy as np
import matplotlib.pyplot as plt
r = np.array([11.7,12.1,10.1,6.6,3.1,1.5,2.3,5.2,
8.7,11.5,12,10.1,6.6,3.3,1.5,2.3,5.3,9.2,11.9])
theta =np.array([0,20,40,60,80,100,120,140,160,180,
200,220,240,260,280,300,320,340,360], dtype=float) # making sure it is float
# convert to radians
# theta *= np.pi/180.
theta = np.deg2rad(theta)
ax = plt.subplot(111, projection='polar')
ax.plot(theta,r)
ax.grid(True)
ax.set_title("A line plot on a polar axis", va='bottom')
plt.show()
I have not managed to find any place in the documentation, where this is explicitly stated (only examples where it is done correctly).
The weird pictures you got came from the fact that all values above 2pi are folded back into the range 0...2pi. So e.g. 20 % 2pi = 1.15
, which is about 65 degrees when converted, which is where the second value is actually located in your plot.
Upvotes: 3