How To Draw Multiple Levels Groupby Histograms In Python?
I have a dataframe like this, col1 col2 col3 A east 5 A west 7 A east 1 A east 6 B east 2 B
Solution 1:
Using seaborn
:
pandas.DataFrame.sort_values
oncol1
will also order the plot sequence fromA
toZ
Data:
import string
import numpy as np
import pandas as pd
import random
alpha_list = [random.choice(list(string.ascii_uppercase)) for _ inrange(10_000)]
coor_list = [random.choice(['east', 'west']) for _ inrange(10_000)]
rand_val = [np.random.randint(10) for _ inrange(10_000)]
df = pd.DataFrame({'col1': alpha_list, 'col2': coor_list, 'col3': rand_val})
df.sort_values(by='col1', inplace=True)
col1 col2 col3
A west 1
A east 3
A west 9
A west 5
A west 7
A east 1
A east 5
A west 2
A east 2
A west 2
Plot:
- Select the number displayed per row with
col_wrap
- Building structured multi-plot grids
seaborn.FacetGrid
seaborn.distplot
g = sns.FacetGrid(df, col='col1', hue='col2', col_wrap=7)
g.map(sns.distplot, 'col3', hist_kws=dict(edgecolor='black'), bins=range(0, 11, 1), kde=False)
plt.xlabel('Value Range')
plt.ylabel('Frequency')
plt.legend()
plt.xticks(range(1, 11, 1))
plt.show()
- if you want tick labels on each graph, put the following code on the line before
plt.show()
:
for ax in g.axes.flatten():
ax.tick_params(labelbottom=True)
Solution 2:
You can pivot the table and groupby plot:
df = pd.DataFrame({'col1': np.random.choice(('A','B'), size=100),
'col2': np.random.choice(('east', 'west'), size=100),
'col3': np.random.randint(0,10, 100)})
df['idx'] = df.groupby('col1').col2.cumcount()
(df.pivot_table(index=['idx','col1'],
columns=['col2'],
values='col3')
.groupby('col1').plot.hist(subplots=False, alpha=0.5)
)
Output:
Post a Comment for "How To Draw Multiple Levels Groupby Histograms In Python?"