Home > OS >  Group years by decade in seaborn barplot
Group years by decade in seaborn barplot

Time:02-21

If I have a DataFrame with a column 'Year' and another column 'Average temperature' and I want to represent them in a barplot to see if the global average temperature has risen over the last decades, how do you convert years to decades?

For example, between 1980 and 1989 I need it to be represented in x axis as 1980. For 1990 and 1999 as 1990, and so on.

Note that:

x axis = Year

y axis = Average temperature

Many thanks

CodePudding user response:

You can do this by,

  1. Converting years to starting year of the decade
  2. Then take average temperature for years of that decade

Code:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

#Sample data. Replace it with your data.
df = pd.DataFrame([[2011,20],[2012,10],[2013,10],[2014,10],[2015,10],[2016,10],[2017,10],[2018,10],[2019,10],[2020,10],[2021,10],[2022,15]], columns=['year','temp'])
df['year'] = df['year'] - df['year'] % 10
df_decade = (df.groupby(['year']).mean().reset_index())
ax = sns.barplot(x="year", y="temp", data=df_decade)
plt.show()
  • Related