Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Plots a series using the Seaborn histogram plot with kde and, mean, mode, and median lines in Jupyter / iPython
## handy multi-plot function for showing mode, median, and mean lines in a distplot
## (but using histplot since distplot is deprecated)
## Author - Abram Flansburg
## Intended for use in Jupyter / iPython
def skewness_plot(series):
Plots a series using the histogram plot with kde and plots, mean, mode, and median lines.
*** Dependencies ***
Series must be a pandas.Series
Seaborn must be imported as sns
matplotlib.pyplot must be imported as plt
sns.set_style("whitegrid", {'axes.grid' : False})
fig, ax = plt.subplots()
# this could probably be done in less lines somehow....
mean_line = ax.twinx()
med_line = mean_line.twinx()
mode_line = med_line.twinx()
mean_line.plot([series.mean(),series.mean()], ax.get_ylim(), scaley=False, color='red')
mean_line.legend([f"Mean = {round(series.mean(),2)}"])
med_line.plot([series.median(),series.median()], ax.get_ylim(), scaley=False, color='green',linestyle="dotted")
med_line.legend([f"Median = {round(series.median(),2)}"],loc=1,bbox_to_anchor=(1,.95))
mode_line.plot([series.mode(),series.mode()], ax.get_ylim(), scaley=False, color='yellow',linestyle="dashed")
mode_line.legend([f"Mode = {series.mode().iloc[0]}"],loc=1,bbox_to_anchor=(1,.90))
sns.histplot(series,bins=10, ax=ax, kde=True);
# Using ^ in place of .distplot - just need to add kde=True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment