Polar chart of annual data with matplolib

I have been trying to plot data for a whole year as a polar plot in matplotlib and have not been able to find any examples of this. I have managed to convert dates from pandas according to this thread , but I cannot wrap my head (literally) with the y or theta axis.

This is how far I got:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd

times = pd.date_range("01/01/2016", "12/31/2016")
rand_nums = np.random.rand(len(times),1)
df = pd.DataFrame(index=times, data=rand_nums, columns=['A'])

ax = plt.subplot(projection='polar')
ax.set_theta_direction(-1)
ax.set_theta_zero_location("N")
ax.plot(mdates.date2num(df.index.to_pydatetime()), df['A'])
plt.show()

      

which gives me this plot

shortening the date range to understand what is going on times = pd.date_range("01/01/2016", "01/05/2016")

I am getting this plot

I understand the start of the series is 90 to 135, but how can I "remap" this so that my year date range starts and ends in the north?

+3


source to share


1 answer


The range of angles of the polar region is in a full circle in radiants, i.e. [0, 2π]

... Therefore, it would be necessary to normalize the date range to a full circle.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd

times = pd.date_range("01/01/2016", "12/31/2016")
rand_nums = np.random.rand(len(times),1)
df = pd.DataFrame(index=times, data=rand_nums, columns=['A'])

ax = plt.subplot(projection='polar')
ax.set_theta_direction(-1)
ax.set_theta_zero_location("N")

t = mdates.date2num(df.index.to_pydatetime())
y = df['A']
tnorm = (t-t.min())/(t.max()-t.min())*2.*np.pi
ax.fill_between(tnorm,y ,0, alpha=0.4)
ax.plot(tnorm,y , linewidth=0.8)
plt.show()

      



enter image description here

+3


source







All Articles