Issue
I'm trying to plot a bar graph showing departure from the mean. I looked through seaborn's documentation but have not figured out how I can plot the bar graph where its y-axis starts at the mean of the dataset and not 0. Essentially, I'm looking at how I can replicate this graph, but using seaborn.
Example Dataset:
{'Year': {0: 2000, 1: 2001, 2: 2002, 3: 2003, 4: 2004, 5: 2005, 6: 2006, 7: 2007, 8: 2008, 9: 2009, 10: 2010, 11: 2011, 12: 2012, 13: 2013, 14: 2014, 15: 2015, 16: 2016, 17: 2017, 18: 2018, 19: 2019, 20: 2020}, 'Temperature (Degrees F)': {0: 64.87, 1: 64.89, 2: 65.55, 3: 64.11, 4: 61.63, 5: 63.83, 6: 63.46, 7: 65.02, 8: 63.3, 9: 62.14, 10: 64.86, 11: 64.98, 12: 66.25, 13: 65.27, 14: 63.49, 15: 64.77, 16: 64.49, 17: 65.65, 18: 64.8, 19: 64.74, 20: 65.28}}
Current Code:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df_temp = pd.read_csv(r"C:\Users\rov\Desktop\CO\temp.csv", usecols=['Year', 'Temperature (Degrees F)'])
sns.set(style='darkgrid')
sns.set_context('talk')
fig, (ax1) = plt.subplots(nrows=1, figsize=(15,5))
sns.barplot(x='Year', y='Temperature (Degrees F)', data=df_temp, color='#3792CB', edgecolor= 'black', ax=ax1)
Solution
Internally, seaborn.barplot
calles matplotlib.axes.Axes.bar
, and any keyword arguments not recognised by seaborn.barplot
are passed through to the matplotlib bar
method (see the seaborn barplot
documentation for more information, specifically the **kwargs
argument).
This second function has an argument bottom
, which indicates the y coordinate of the base of the bar (see the matplotlib bar
documentation for more information). You can set this bottom
argument equal to the mean temperature value. However, the bar heights are determined relative to the bottom, so you should also subtract the mean temperature value from the bar heights. One example of how to achieve this is given below.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df_temp = pd.read_csv("temp.csv", usecols=['Year', 'Temperature (Degrees F)'])
# calculate the mean temperature and subtract it from the dataset
mean = df_temp['Temperature (Degrees F)'].mean()
df_temp['Temperature (Degrees F)'] -= mean
sns.set(style='darkgrid')
sns.set_context('talk')
fig, (ax1) = plt.subplots(nrows=1, figsize=(15,5))
sns.barplot(x='Year', y='Temperature (Degrees F)', data=df_temp,
color='#3792CB', edgecolor= 'black', ax=ax1,
bottom=mean) # <-- set the bottom of the bars
Answered By - luuk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.