Last active
March 7, 2023 00:51
-
-
Save camriddell/77a698010398056550bc583efd8416be to your computer and use it in GitHub Desktop.
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
```python | |
from matplotlib.pyplot import figure, subplots, show, rc, rcdefaults | |
from numpy.random import default_rng | |
from numpy import linspace, atleast_2d | |
from pandas import DataFrame | |
from scipy.stats import gaussian_kde | |
from seaborn import stripplot, rugplot | |
rcdefaults() | |
rc('font', size=14) | |
rc('axes.spines', left=False, top=False, right=False) | |
rng = default_rng(0) | |
df = DataFrame({ | |
'A': rng.normal(40, 3, size=300), | |
'B': rng.normal(65, 5, size=300), | |
'C': rng.normal(50, 10, size=300), | |
}) | |
fig = figure(figsize=(12, 8)) | |
gs = fig.add_gridspec(2, 2, hspace=.1, top=0.8) | |
subfigs = { | |
'unnecessarily complex': fig.add_subfigure(gs[0, 0]), | |
'classic': fig.add_subfigure(gs[0, 1]), | |
'indecisive': fig.add_subfigure(gs[1, 0]), | |
'minimalist': fig.add_subfigure(gs[1, 1]) | |
} | |
for i, col_name in enumerate(df.columns): | |
ax = subfigs['unnecessarily complex'].add_subplot(1, len(df.columns), i+1) | |
s = df[col_name] | |
# histogram | |
ax.hist(s, bins='auto', orientation='horizontal', ec='white', fc='tab:blue', alpha=.7, density=True) | |
# violin | |
kde = gaussian_kde(s) | |
grid = linspace(s.min(), s.max()) | |
art_vio = ax.fill_betweenx(grid, -kde(grid), alpha=.7) | |
# point/stick | |
mean = s.mean() | |
stdev = s.std() | |
art_std, = ax.plot([0, 0], [mean - stdev, mean + stdev], color='tab:orange', lw=2) | |
art_avg = ax.scatter([0], mean, fc='white', ec='tab:orange', linewidth=2, zorder=99) | |
ax = subfigs['classic'].add_subplot(1, len(df.columns), i+1) | |
ax.boxplot(s, notch=True) | |
ax = subfigs['indecisive'].add_subplot(1, len(df.columns), i+1) | |
stripplot(y=s, ax=ax, size=4, jitter=0.25, ec='white', alpha=.7) | |
ax.set_ylabel('') | |
ax.set_xlabel(col_name) | |
ax.axhline(mean - stdev, .2, .8, color='tab:orange', zorder=6, lw=3) | |
ax.axhline(mean + stdev, .2, .8, color='tab:orange', zorder=6, lw=3) | |
ax.axhline(mean, .2, .8, color='tab:orange', ls='--', zorder=6, lw=3) | |
ax = subfigs['minimalist'].add_subplot(1, len(df.columns), i+1) | |
ax.eventplot(s, orientation='vertical', alpha=.3) | |
for ax in fig.axes: | |
ax.set_xticks([]) | |
for title, sfig in subfigs.items(): | |
sfig.suptitle(title.title()) | |
for col_name, ax in zip(df.columns, sfig.axes): | |
ax.set_xlabel(col_name) | |
for ax in sfig.axes[1:]: | |
ax.yaxis.set_visible(False) | |
sfig.subplots_adjust(wspace=0) | |
fig.subfigs[0].text( | |
0.5, 1, va='bottom', s='Which Univariate Plot Are You?', transform=fig.transFigure, ha='center', fontsize='xx-large' | |
) | |
from itertools import islice | |
for sfig in islice(subfigs.values(), 0, None, 3): | |
sfig.set_facecolor('gainsboro') | |
fig.savefig('univariateplot.png', bbox_inches='tight') | |
show() | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment