Skip to content

Instantly share code, notes, and snippets.

@ksv-muralidhar
Last active February 16, 2021 12:45
Show Gist options
  • Save ksv-muralidhar/da21d460e190ea288430290380817885 to your computer and use it in GitHub Desktop.
Save ksv-muralidhar/da21d460e190ea288430290380817885 to your computer and use it in GitHub Desktop.
histogram bins
fig,ax = plt.subplots(2,3,figsize=(15,10))
row = col = 0
np.random.seed(11)
norm_dist = np.random.randn(1000)
for n,i in enumerate(np.linspace(5,100,6)):
if (n>0) & (n%3==0):
row += 1
col = 0
sns.histplot(x=norm_dist,bins=int(i),ax=ax[row,col])
ax[row,col].set_title(f'bins = {int(i)}')
col += 1
np.random.seed(11)
norm_dist = np.random.randn(1000)
bin_count = int(np.ceil(np.log2(len(norm_dist))) + 1)
fig = plt.figure(figsize=(7,7))
sns.histplot(x=norm_dist,bins=bin_count)
plt.title(f'bins = {bin_count}')
import pandas as pd
np.random.seed(11)
norm_dist = pd.Series(np.random.randn(1000))
q1 = norm_dist.quantile(0.25)
q3 = norm_dist.quantile(0.75)
iqr = q3 - q1
bin_width = (2 * iqr) / (len(norm_dist) ** (1 / 3))
bin_count = int(np.ceil((norm_dist.max() - norm_dist.min()) / bin_width))
fig = plt.figure(figsize=(7,7))
sns.histplot(x=norm_dist,bins=bin_count)
plt.title(f'bins = {bin_count}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment