2017-07-18 4 views
0

Ich versuche, mit Seaborn ein Balkendiagramm zu erstellen, aber wenn ich die Diagramme staple, sind die Balkenbreiten ungleichmäßig. Ich möchte sie alle gleich machen, oder besser, die erste größer als die anderen (die eine Zerlegung der ersten sind).Balkenbreite beim Stapeln ändern Seaborn distplot

Hier ist ein Mock-Probe ein mein 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(); 

Was ich erhalte, ist diese Zahl:

Uneven bars

Wie verwalte ich Breite? Ich dachte, es wurde automatisch von Bins gesetzt, aber wenn alle Bins gleich sind, wird das Problem nicht gelöst.

+0

Einstellung der gleichen Anzahl von Bins ist nicht das Gleiche wie die Einstellung der gleichen Breite der Bins der Bereiche der Daten sind unterschiedlich. – mwaskom

+0

OK, ich verstehe. Also benutze ich 'hist_kws = {" range ": [min_NbGals, max_NbGals]}' und es funktioniert. Prost! – Matt

Antwort

0

OK, dank mwaskom, es funktioniert. Dies ist der richtige 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(); 
+0

Das ist in Ordnung ... aber "Bins" können auch eine Reihe von Fachkanten sein ... – mwaskom