1

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  

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.

1 Answer 1

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

enter image description here


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()
Sign up to request clarification or add additional context in comments.

8 Comments

Nice! I'd like to make it without writing everytime a line because i have to use many columns but it's good anyway! I'm plotting in the jupiter notebook and i ask you if there is a way to make the subplots one after the other and not side by side (i mean figures with the largest x-axis and small y-axis)
This is an important piece of information that should be part of the question. I updated the answer.
I tried with edited code but i receive an error to this line 'ax.plot(df.Date, df["Influenza[it]"], label="Influenza[it]")' : 'ValueError: invalid literal for float(): 2016-16'. In case i run 'df['Date'] = pd.to_datetime(df['Date'])' i receive this error 'ValueError: month must be in 1..12'
Sorry, I forgot to remove the comment: Using matplotib you need to use datetimes - it's not optional anymore. The error that you get, makes sense, since "2016-16" is no valid date. Are you expecting that matplotlib invents 4 new months in the year or did I misunderstand the datetime format you are using?
Well that is.. another piece of information that should be part of the question. Is your data covering several years? Are dates equally spaced, i.e. is every week between the first and last entry present? Would you rather have the date axes in weeks or in normal dates (like every monday of that specific week)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.