pandas :将多个时间序列DataFrame绘制到单个图中 [英] Pandas: plot multiple time series DataFrame into a single plot

查看:374
本文介绍了 pandas :将多个时间序列DataFrame绘制到单个图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下熊猫DataFrame:

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

这是一个数据帧,具有从min=1max=35的多个时间序列查询数据.每个Group都有这样的时间序列.

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

我想在1到35的x轴上绘制从A到Z的每个时间序列.y轴每次都是blocks.

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

我的问题:如何格式化此数据框以绘制多个序列?列应为GroupAGroupB等吗?

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?

编辑

如果我尝试:

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

x轴完全不正确.所有时间序列都应从0到35绘制,并一一绘制.

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

我该如何解决?

推荐答案

查看此变体.第一个是安德鲁斯曲线,第二个是多线图,按一列Month分组.数据框data包括三列TemperatureDayMonth:

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.

这篇关于 pandas :将多个时间序列DataFrame绘制到单个图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆