Pandas: plot multiple time series DataFrame into a single plot

浪子不回头ぞ 提交于 2019-11-30 22:44:09

问题


I have the following pandas DataFrame:

     time      Group      blocks
0     1        A           4
1     2        A           7
2     3        A           12
3     4        A           17
4     5        A           21 
5     6        A           26
6     7        A           33
7     8        A           39
8     9        A           48
9     10       A           59
    ....        ....          ....
36     35      A           231
37     1       B           1
38     2       B           1.5
39     3       B           3
40     4       B           5
41     5       B           6
    ....        ....          ....
911    35      Z           349

This is a dataframe with multiple time series-ques data, from min=1 to max=35. Each Group has a time series like this.

I would like to plot each individual time series A through Z against an x-axis of 1 to 35. The y-axis would be the blocks at each time.

I was thinking of using something like an Andrews Curves plot, which would plot each series against one another. Each "hue" would be set to a different group. (Other ideas are welcome.)

My problem: how do you format this dataframe to plot multiple series? Should the columns be GroupA, GroupB, etc.?

How do you get the dataframe to be in the format:

time GroupA blocksA GroupsB blocksB GroupsC blocksC....

Is this the correct format for an Andrews plot as shown?

EDIT

If I try:

df.groupby('Group').plot(legend=False)

the x-axis is completely incorrect. All time series should be plotted from 0 to 35, all in one series.

How do I solve this?


回答1:


Look at this variants. The first is Andrews' curves and the second is a multiline plot which are grouped by one column Month. The dataframe data includes three columns Temperature, Day, and Month:

import pandas as pd
import statsmodels.api as sm
import matplotlib.pylab as plt
from pandas.tools.plotting import andrews_curves

data = sm.datasets.get_rdataset('airquality').data
fig, (ax1, ax2) = plt.subplots(nrows = 2, ncols = 1)
data = data[data.columns.tolist()[3:]] # use only Temp, Month, Day

# Andrews' curves
andrews_curves(data, 'Month', ax=ax1)

# multiline plot with group by
for key, grp in data.groupby(['Month']): 
    ax2.plot(grp['Day'], grp['Temp'], label = "Temp in {0:02d}".format(key))
plt.legend(loc='best')    
plt.show()

When you plot Andrews' curve your data salvaged to one function. It means that Andrews' curves that are represented by functions close together suggest that the corresponding data points will also be close together.




回答2:


You can re-structure the data as a pivot table:

df.pivot_table(index='time',columns='Group',values='blocks',aggfunc='sum').plot()


来源:https://stackoverflow.com/questions/38197964/pandas-plot-multiple-time-series-dataframe-into-a-single-plot

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