Search code examples
pythonimageplotpolar-coordinates

Making a polar plot with points every 5 degrees?


I would like to make a polar plot were there is a unique value plotted at every 5 degrees on a degree circle. I don't really know what the correct question to ask is but I am running into trouble. Here is my current code:

import numpy as np
import matplotlib.pyplot as plt
import csv

r = csv.reader(open('data.csv'))
#data is a list of 73 data points taken at each 5 degree increment
theta = (0,360,5)

#plot image
img = plt.imread("voltage_abs.png")
fig, ax = plt.subplots()
ax.imshow(img)
ax.imshow(img)
ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
ax.set_rmax(2)
ax.set_rticks([0.5, 1, 1.5, 2])  # less radial ticks
ax.set_rlabel_position(-22.5)  # get radial labels away from plotted line
ax.grid(True)

ax.set_title("Polar", va='bottom')
plt.show()

My errors are either simply nothing showing up on the plot, or an issue with the sizes of r and theta not matching. Any help is appreciated!


Solution

  • You are not using the csv reader correctly. Instead use numpy's genfromtxt function. I made my own data.csv file that is 73 numbers increasing by 2.

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    r =  np.genfromtxt('data.csv',delimiter=',')
    #data is a list of 73 data points taken at each 5 degree increment
    theta = np.linspace(0, 2 * np.pi, 73)
    
    ax = plt.subplot(111, projection='polar')
    ax.plot(theta, r)
    ax.set_rmax(2)
    ax.set_rticks([0.5, 1, 1.5, 2])  # less radial ticks
    ax.set_rlabel_position(-22.5)  # get radial labels away from plotted line
    ax.grid(True)
    
    ax.set_title("Polar", va='bottom')
    plt.show()
    

    enter image description here