I'm trying to make a barchart with Seaborn, but when I stack the plots, bar width are uneven. I'd like to make them all the same, or better, the first one larger than the others (which are a decomposition of the first).
Here is a mock sample a my code:
import pandas as pd
import seaborn as sns
Groups= pd.DataFrame([['E', 5L],['S0', 5L],['S', 4L],['S', 6L],['E', 4L],['S', 4L],
['E', 4L],['S', 4L],['S', 4L],['S0', 4L],['E', 5L],['S', 4L],['S', 4L],
['S', 4L],['E', 4L],['E', 5L],['E', 4L],['S0', 4L],['E', 4L],['S', 5L],
['E', 4L],['E', 4L],['S', 4L],['E', 4L],['S', 4L],['E', 4L],['E', 4L],
['S', 4L],['E', 4L],['E', 4L],['S0a', 6L],['E', 4L],['S0', 4L],['S0a', 4L],
['E', 5L],['E', 4L],['S0', 4L],['S', 6L],['S', 4L],['E', 4L],['E', 5L],
['E', 4L],['E', 4L],['E', 5L],['S', 5L]], columns=['MorphCen', 'NbGal'])
shift = 0.12
local_bins = (CompactGroups_raw['NbGal'].max()-CompactGroups_raw['NbGal'].min()+1)*10
ax1=sns.distplot(Groups['NbGal'], bins=local_bins, kde=False,rug=False, label="All")
ax1=sns.distplot(Groups['NbGal'].loc[Groups['MorphCen']=='S']+shift,
bins=local_bins, kde=False,rug=False,color='b', label="$S$ central")
ax1=sns.distplot(Groups['NbGal'].loc[Groups['MorphCen']=='E']+2*shift,
bins=local_bins, kde=False,rug=False,color='r', label="$E$ central")
ax1=sns.distplot(Groups['NbGal'].loc[Groups['MorphCen']=='S0']+3*shift,
bins=local_bins, kde=False,rug=False,color='g', label="$S_0$ central")
ax1=sns.distplot(Groups['NbGal'].loc[~Groups['MorphCen'].isin(['S','E','S0'])]+4*shift,
bins=local_bins, kde=False,rug=False, color='y', label="Other central")
ax1.set(xlim=[Groups['NbGal'].min(), Groups['NbGal'].max()+1]);
ax1.set_ylabel('Object number')
loc1 = plticker.MultipleLocator(base=1.0)
ax1.xaxis.set_major_locator(loc1)
ax1.legend();
What I get is this figure:
How do I manage width? I thought it was automatically set by bins, but having all bins the same doesn't solve the issue.
OK, thanks to mwaskom, it works. This is the proper code:
min_NbGals = CompactGroups_raw['NbGal'].min()
max_NbGals = CompactGroups_raw['NbGal'].max()
local_bins = (max_NbGals-min_NbGals+1)*10
ax1=sns.distplot(CompactGroups_raw['NbGal'], bins=local_bins, hist_kws={"range": [min_NbGals,max_NbGals]}, kde=False,rug=False, color='k', label="All")
ax1=sns.distplot(CompactGroups_raw['NbGal'].loc[CompactGroups_raw['MorphCen']=='S']+shift,
bins=local_bins, hist_kws={"range": [min_NbGals,max_NbGals]}, kde=False,rug=False,color='b', label="$S$ central")
ax1=sns.distplot(CompactGroups_raw['NbGal'].loc[CompactGroups_raw['MorphCen']=='E']+2*shift,
bins=local_bins, hist_kws={"range": [min_NbGals,max_NbGals]}, kde=False,rug=False,color='r', label="$E$ central")
ax1=sns.distplot(CompactGroups_raw['NbGal'].loc[CompactGroups_raw['MorphCen']=='S0']+3*shift,
bins=local_bins, hist_kws={"range": [min_NbGals,max_NbGals]}, kde=False,rug=False,color='g', label="$S_0$ central")
ax1=sns.distplot(CompactGroups_raw['NbGal'].loc[~CompactGroups_raw['MorphCen'].isin(['S','E','S0'])]+4*shift,
bins=local_bins, hist_kws={"range": [min_NbGals,max_NbGals]}, kde=False,rug=False, color='y', label="Other central")
ax1.set(xlim=[CompactGroups_raw['NbGal'].min(), CompactGroups_raw['NbGal'].max()+1]);
ax1.legend();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.