Python polar plot: Plot values corresponding to the angle

天大地大妈咪最大 提交于 2021-01-28 03:56:05

问题


I'm trying to plot sensor data, which was recorded with different angles.

 import pandas as pd
 import matplotlib.pyplot as plt

 #create dataframe, each row contains an angle and a corresponding value
 df = pd.DataFrame(columns = ["angle","value"])
 df.angle = [0,5,10,15,20,25,30,35,40,45,50,55,60]
 df['value'] = df.apply(lambda row: 100 -row.angle/2 , axis=1)
 print(df)

 #plotting
 plt.polar(df['angle'].values,df['value'].values)
 plt.show()

This returns:

But I need a plot that shows a value of 100 at zero degrees, 97.5 at 5 degrees and so on.


回答1:


theta in plt.polar(theta, r) needs to be in radians. You can make a new column converting the angle to radians using the following:

import math
df['rad'] = df.apply(lambda row: row.angle*math.pi/180, axis=1)
plt.polar(df['rad'], df['value'])

This results in this plot:.



来源:https://stackoverflow.com/questions/56231994/python-polar-plot-plot-values-corresponding-to-the-angle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!