0

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!

K. Schneider
  • 21
  • 11
  • In the code you posted, the variable `theta` is a tuple with three elements, which I think is not what you want it to be. It also needs to be in radians. It should be something like `theta = np.linspace(0, 2 * np.pi, 73)`. – Josh Karpel Aug 22 '17 at 03:11
  • So I made that change and received this error: `TypeError: object of type '_csv.reader' has no len()`. Do I need to save this variable as an array? – K. Schneider Aug 22 '17 at 03:22
  • Thats because r is not a list as you say it it... Its a csv.reader object. You have to iterate over it in order to get the values, which in your case is a single row of data points... Just do ```r= list( csv.reader( open('data.csv','r') ) )[0]``` – dermen Aug 22 '17 at 04:24
  • Now I have this error: `ValueError: x and y must have same first dimension, but have shapes (73,) and (2,)` – K. Schneider Aug 22 '17 at 20:04
  • I got this to work, thank you! – K. Schneider Aug 22 '17 at 20:42
  • I am also now trying to add a picture in the background of this plot. I updated my code above in the original question, but cannot get the plot on top of the image. Any advice here? – K. Schneider Aug 23 '17 at 02:28

1 Answers1

2

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

BenT
  • 3,172
  • 3
  • 18
  • 38
  • So I have the error: `ValueError: x and y must have same first dimension, but have shapes (73,) and (1,)` I'm guessing this is in my cvs file somewhere? It's currently 1 column of 73 values. – K. Schneider Aug 22 '17 at 20:07
  • Do a print r.shape() and see what happens. You may need to transpose the data i.e. r.T – BenT Aug 22 '17 at 20:38
  • I am also now trying to add a picture in the background of this plot. I updated my code above in the original question, but cannot get the plot on top of the image. Any advice here? – K. Schneider Aug 23 '17 at 02:26
  • That question has been answered [here](https://stackoverflow.com/questions/5073386/how-do-you-directly-overlay-a-scatter-plot-on-top-of-a-jpg-image-in-matplotlib). If you are still having issues submit another question. – BenT Aug 23 '17 at 03:13