Last active
December 5, 2020 21:54
-
-
Save pcmasuzzo/d7f69b7328ed0310da29e9a4ad9a80b5 to your computer and use it in GitHub Desktop.
Some useful seaborn snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
# white background in plots | |
sns.set(style="whitegrid") | |
# df is a pandas dataframe | |
# plot by column by variable 'var1', in 3 columns, coloring by variable 'var2' | |
grid = sns.FacetGrid(df, col='var1', col_wrap=3, hue='var2', size=4, palette='Set2') | |
# map a scatterplot (x, y) | |
grid.map(plt.plot, 'x', 'y', marker="o", ms=1) | |
plt.subplots_adjust(top=0.85) | |
# add a supertitle to the plot | |
grid.fig.suptitle('Here goes the title'); | |
# rotate x axis labels, given a grid facet object | |
g.set_xticklabels(rotation=90) | |
# do not share x and y axes range | |
grid = sns.FacetGrid(df, col='var1', col_wrap=3, hue='var2', size=4, palette='Set2', sharey = False, sharex = False) | |
# (re)adjust fontscale for axes labels etc | |
sns.set(style="whitegrid", font_scale=1.0) | |
# as above | |
grid = sns.FacetGrid(df, size=3, palette=sns.color_palette('Paired', 10), sharey=False, | |
col='var1', col_wrap=5, hue='var2') | |
# map a histogram of variable 'var3' | |
grid.map(plt.hist, 'var3', bins = 10, edgecolor='white') | |
# rotate the labels of x axis by 90 deg | |
grid.set_xticklabels(rotation=90) | |
# map a density plot (KDE) | |
g.map(sns.distplot, "var3"); | |
# map a density plot but donfit and show 'only' a normal distribution | |
g.map(sns.distplot, "var3", fit=norm, kde=False); | |
# mix seaborn with more canonical matplotlib | |
# two plots that share the x axis | |
fig = plt.figure(figsize=(5,5)) | |
ax1 = plt.subplot(311) | |
plt.plot(df.t, df.x) | |
plt.ylabel('x', fontsize=14) | |
ax2 = plt.subplot(312, sharex=ax1) | |
plt.plot(df.t, df.y) | |
plt.ylabel('y', fontsize=14) | |
plt.xlabel('t') | |
ax1.set_title('title for the plot') | |
plt.show() | |
# g facet Grid - make x ticks invisible | |
g.set(xticks=[]) | |
# plot heatmaps of correlation matrices | |
# facet by column by variable 'var1, 3 columns | |
g = sns.FacetGrid(df, col='var1', size=8, col_wrap=3) | |
g.map_dataframe(lambda df, color: sns.heatmap(df.corr(), linewidths=0, square=True, cmap='coolwarm')); | |
# plot a clustermap | |
cm = sns.clustermap(df.corr(), square=True, linewidths=.5, cmap='coolwarm') | |
plt.setp(cm.ax_heatmap.yaxis.get_majorticklabels(), rotation=0); | |
# change matplotlib boxpolot colors | |
fig, ax1 = plt.subplots(figsize=(10, 8)) | |
bp = plt.boxplot(df, patch_artist=True) | |
for i in range(len(bp['boxes'])): | |
bp['boxes'][i].set(facecolor = sns.color_palette("Paired", 15)[i]) | |
# rotate the x ticks by 90 deg | |
xtickNames = plt.setp(ax1, xticklabels=data.columns) | |
plt.setp(xtickNames, rotation=90, fontsize=12); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment