简体   繁体   English

堆叠Seaborn distplot时条宽变化

[英]Bar width changing when stacking Seaborn distplot

I'm trying to make a barchart with Seaborn, but when I stack the plots, bar width are uneven. 我正在尝试与Seaborn制作条形图,但是当我堆叠图时,条形宽度不均匀。 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. 好的,感谢mwaskom,它可以工作。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM