年度数据的极坐标图与matplolib

时间:2017-05-02 12:28:32

标签: python matplotlib time-series polar-coordinates

我试图将整年的数据绘制为matplotlib中的极坐标图,并且无法找到任何这样的示例。我设法根据this thread转换大熊猫的日期,但是我无法绕着(沿着)y轴或theta包裹我的头。

这是我走了多远:

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()

给了我这个情节:

this plot

减少日期范围以了解正在发生的事情 times = pd.date_range("01/01/2016", "01/05/2016")我得到了这个情节:

this plot

我认为系列的开头是在90到135之间,但我怎样才能“重新映射”这个以便我的年份日期范围从北方起源开始并结束?

1 个答案:

答案 0 :(得分:4)

极坐标图的角度范围在辐射中的整圆上,即[0, 2π]。  因此,需要将日期范围标准化为整圆。

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