在同一图中绘制不同的数据aframes

时间:2021-06-24 12:02:19

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

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

每年都有不同的数字,记录的时间,所以熊猫的日期时间指数都是不同的。

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?

我想把不同年份的数据画在同一个图中进行比较。x轴是1到12,y轴是温度。我该怎么做呢?

4 个解决方案

#1


20  

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:

虽然Chang的回答解释了如何在同一个图形上绘制多次,但在这种情况下,你最好使用groupby和unstack:

(Assuming you have this in dataframe, with datetime index already)

(假设您在dataframe中有这个,并且已经有datetime索引)

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

#2


212  

Try:

试一试:

ax = df1.plot()
df2.plot(ax=ax)

#3


10  

If you a running Jupyter/Ipython notebook and having problems using;

如果你有一个正在运行的Jupyter/Ipython笔记本,并且有使用问题;

ax = df1.plot()

ax = df1.plot()

df2.plot(ax=ax)

df2.plot(ax = ax)

Run the command inside of the same cell!! It wont, for some reason, work when they are separated into sequential cells. For me at least.

在同一个单元格中运行命令!!由于某些原因,当它们被分割成连续的细胞时,它就不起作用了。至少对我来说。

#4


1  

To do this for multiple dataframes, you can do a for loop over them:

对于多个dataframes,可以对它们执行一个for循环:

fig = plt.figure(num=None, figsize=(10, 8))
ax = dict_of_dfs['FOO'].column.plot()
for BAR in dict_of_dfs.keys():
    if BAR == 'FOO':
        pass
    else:
        dict_of_dfs[BAR].column.plot(ax=ax)

#1


20  

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:

虽然Chang的回答解释了如何在同一个图形上绘制多次,但在这种情况下,你最好使用groupby和unstack:

(Assuming you have this in dataframe, with datetime index already)

(假设您在dataframe中有这个,并且已经有datetime索引)

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

#2


212  

Try:

试一试:

ax = df1.plot()
df2.plot(ax=ax)

#3


10  

If you a running Jupyter/Ipython notebook and having problems using;

如果你有一个正在运行的Jupyter/Ipython笔记本,并且有使用问题;

ax = df1.plot()

ax = df1.plot()

df2.plot(ax=ax)

df2.plot(ax = ax)

Run the command inside of the same cell!! It wont, for some reason, work when they are separated into sequential cells. For me at least.

在同一个单元格中运行命令!!由于某些原因,当它们被分割成连续的细胞时,它就不起作用了。至少对我来说。

#4


1  

To do this for multiple dataframes, you can do a for loop over them:

对于多个dataframes,可以对它们执行一个for循环:

fig = plt.figure(num=None, figsize=(10, 8))
ax = dict_of_dfs['FOO'].column.plot()
for BAR in dict_of_dfs.keys():
    if BAR == 'FOO':
        pass
    else:
        dict_of_dfs[BAR].column.plot(ax=ax)