Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot DataFrame in 1 year period

I have dataframe:

                    temp_old                                            temp_new
Year                2013        2014    2015    2016    2017    2018    2013    2014    2015    2016    2017    2018
Date                                                

2013-01-01 23:00:00 21.587569   NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
2013-01-02 00:00:00 21.585347   NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
2013-01-02 01:00:00 21.583472   NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN
... ... ... ... ... ... ... ... ... ... ... ... ...
2018-02-05 00:00:00 NaN         NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     22.882083
2018-02-05 01:00:00 NaN         NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     NaN     22.878472

When I plot this df thats my result.

enter image description here

My goal is show it but without separate by years. So I want to have 5 curves in range January: December on one chart.

update:(code to plot)

df_sep_by_year.plot(figsize=(15,8))
like image 765
CezarySzulc Avatar asked Dec 08 '25 21:12

CezarySzulc


1 Answers

Simply remove year from your Date column. I mean instead of 2013-01-01 23:00:00 use 01-01 23:00:00 and adjust your data similarly for other records.

# remove datetime index
df.reset_index(inplace=True)
# create new column without year, use ':02d' to correct sorting
df['new_date'] = df.Date.apply(lambda x: '{:02d}-{:02d}-{:02d}:00:00'.format(x.month, x.day, x.hour)) 
# set new index to df
df.set_index('new_date', inplace=True)
# remove old column with datetime
df = df.drop(labels=['Date'], axis=1)
# remove multiindex in columns
df.columns = [''.join(str(col)) for col in df.columns]
# join variable from different year but the same month and day
df = pd.concat([pd.DataFrame(df[x]).dropna(axis=0, how='any') for x in df_sep_by_year], axis=1).dropna(axis=1, how='all')
df.plot()
like image 160
Michal_Szulc Avatar answered Dec 11 '25 12:12

Michal_Szulc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!