在同一图中绘制不同的数据框 [英] Plot different DataFrames in the same figure

查看:72
本文介绍了在同一图中绘制不同的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多年温度记录的温度文件,格式如下:

I have a temperature file with many years temperature records, in a format as below:

2012-04-12,16:13:09,20.6
2012-04-12,17:13:09,20.9
2012-04-12,18:13:09,20.6
2007-05-12,19:13:09,5.4
2007-05-12,20:13:09,20.6
2007-05-12,20:13:09,20.6
2005-08-11,11:13:09,20.6
2005-08-11,11:13:09,17.5
2005-08-13,07:13:09,20.6
2006-04-13,01:13:09,20.6

每年的记录编号和时间都不同,所以熊猫的datetimeindices都不同.

Every year has different numbers, time of the records, so the pandas datetimeindices are all different.

我想在同一图中绘制不同年份的数据以进行比较. X轴为1月至12月,Y轴为温度.我应该怎么做呢?

I want to plot the different year's data in the same figure for comparing . The X-axis is Jan to Dec, the Y-axis is temperature. How should I go about doing this?

推荐答案

尽管Chang的答案说明了如何在同一图形上绘制多次,但在这种情况下,使用

Although Chang's answer explains how to plot multiple times on the same figure, in this case you might be better off in this case using a groupby and unstacking:

(假设您已经在数据框中使用它,并且已经有日期时间索引)

In [1]: df
Out[1]:
            value  
datetime                         
2010-01-01      1  
2010-02-01      1  
2009-01-01      1  

# create additional month and year columns for convenience
df['Month'] = map(lambda x: x.month, df.index)
df['Year'] = map(lambda x: x.year, df.index)    

In [5]: df.groupby(['Month','Year']).mean().unstack()
Out[5]:
       value      
Year    2009  2010
Month             
1          1     1
2        NaN     1

现在很容易绘制(每年以单独的一行):

Now it's easy to plot (each year as a separate line):

df.groupby(['Month','Year']).mean().unstack().plot()

这篇关于在同一图中绘制不同的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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