如何基于相同的日期时间x轴同时绘制两个不同的数据框列 [英] How to plot two different dataframe columns at time based on the same datetime x-axis

查看:106
本文介绍了如何基于相同的日期时间x轴同时绘制两个不同的数据框列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的数据框:

Hi I have a dataframe like this:

        Date  Influenza[it]  Febbre[it]  Cefalea[it]  Paracetamolo[it]  \
0    2008-01            989        2395         1291              2933   
1    2008-02            962        2553         1360              2547   
2    2008-03           1029        2309         1401              2735   
3    2008-04           1031        2399         1137              2296    

     Unnamed: 6 tot_incidence  
0           NaN          4.56  
1           NaN          5.98  
2           NaN          6.54  
3           NaN          6.95  

我想绘制不同的图形,在x轴上的Date列,在y轴上的Influenza[it]列,以及诸如Febbre[it]的另一列.然后再次x轴Date列,y轴Influenza[it]列和另一列(例如Paracetamolo[it]),依此类推.我试图弄清楚是否有一种快速的方法可以在不完全操纵数据帧的情况下进行构建.

I'd like to plot different figures with on x-axis the Date column and the y-axis the Influenza[it] column and another column like Febbre[it]. Then again x-axis the Date column, y-axis Influenza[it] column and another column (ex. Paracetamolo[it]) and so on. I'm trying to figure out if there is a fast way to make it without completely manipulate the dataframes.

推荐答案

您可以简单地绘制3个不同的子图.

You can simply plot 3 different subplots.

import pandas as pd
import matplotlib.pyplot as plt

dic = {"Date" : ["2008-01","2008-02", "2008-03", "2008-04"],
       "Influenza[it]" : [989,962,1029,1031],
        "Febbre[it]" : [2395,2553,2309,2399],
        "Cefalea[it]" : [1291,1360,1401,1137],
        "Paracetamolo[it]" : [2933,2547,2735,2296]}

df = pd.DataFrame(dic)
#optionally convert to datetime
df['Date'] = pd.to_datetime(df['Date'])

fig, ax = plt.subplots(1,3, figsize=(13,7))
df.plot(x="Date", y=["Influenza[it]","Febbre[it]" ], ax=ax[0])
df.plot(x="Date", y=["Influenza[it]","Cefalea[it]" ], ax=ax[1])
df.plot(x="Date", y=["Influenza[it]","Paracetamolo[it]" ], ax=ax[2])

#optionally equalize yaxis limits
for a in ax:
    a.set_ylim([800, 3000])

plt.show()


如果要在jupyter笔记本中分别绘制每个图,则可以执行以下操作.
此外,我们将日期从格式year-week转换为日期时间,以便能够使用matplotlib进行绘制.


If you want to plot each plot separately in a jupyter notebook, the following might do what you want.
Additionally we convert the dates from format year-week to a datetime to be able to plot them with matplotlib.

%matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt

dic = {"Date" : ["2008-01","2008-02", "2008-03", "2008-04"],
       "Influenza[it]" : [989,962,1029,1031],
        "Febbre[it]" : [2395,2553,2309,2399],
        "Cefalea[it]" : [1291,1360,1401,1137],
        "Paracetamolo[it]" : [2933,2547,2735,2296]}

df = pd.DataFrame(dic)
#convert to datetime, format year-week -> date (monday of that week)
df['Date'] = [ date + "-1" for date in df['Date']] # add "-1" indicating monday of that week
df['Date'] = pd.to_datetime(df['Date'], format="%Y-%W-%w")

cols = ["Febbre[it]", "Cefalea[it]", "Paracetamolo[it]"]
for col in cols:
    plt.close()
    fig, ax = plt.subplots(1,1)
    ax.set_ylim([800, 3000])
    ax.plot(df.Date, df["Influenza[it]"], label="Influenza[it]")
    ax.plot(df.Date, df[col], label=col)
    ax.legend()
    plt.show()

这篇关于如何基于相同的日期时间x轴同时绘制两个不同的数据框列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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