从1个图上绘制多个时间序列

时间:2018-12-03 06:59:45

标签: python dataframe matplotlib plot time-series

我有一个DataFrame,看起来像这样:

    date    organization    percent
85  2018-10-01  org1    0.350875
88  2018-10-02  org1    0.341221
... ... ... ...
3961    2018-10-01 org2 0.292511
3964    2018-10-02 org2 0.418349

我需要在同一图中绘制每个组织的时间序列。

我首先尝试对散点图使用最佳拟合线,然后对sns.lmplot和sns.regplot进行尝试,但我似乎只能将所有组织绘制在一起。然后,我在Pandas: plot multiple time series DataFrame into a single plot处找到了这个答案,但是我一直在努力复制格式。我该怎么做呢?谢谢!

1 个答案:

答案 0 :(得分:-1)

这是一种方法:

import matplotlib.dates as mdates
grouped_df = df.groupby('organization')
fig, ax = plt.subplots()
for key, item in grouped_df:
    plt.plot_date(item['date'], item['percent'], '-', label=str(key))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))
ax.xaxis.set_major_locator(mdates.DayLocator(interval=1))
plt.legend()
plt.show()

enter image description here

相关问题