Ale*_*x L 2 python matplotlib histogram seaborn
我正在使用Seaborn的FacetGrid绘制一些直方图,并且我认为自动bin大小调整仅使用每个类别(而不是每个子图)的数据,这会导致一些奇怪的结果(请参阅中的细小绿色bin y = 2
):
g = sns.FacetGrid(df, row='y', hue='category', size=3, aspect=2, sharex='none')
_ = g.map(plt.hist, 'x', alpha=0.6)
Run Code Online (Sandbox Code Playgroud)
有没有办法(使用Seaborn,不退回matplotlib)使每个图的直方图bin大小相等?
我知道我可以手动指定所有bin宽度,但这会迫使所有直方图都在相同的x范围内(请参阅笔记本)。
笔记本:https://gist.github.com/alexlouden/42b5983f5106ec92c092f8a2697847e6
您需要为此定义一个包装函数plt.hist
,以进行色调分组,例如
%matplotlib inline
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
tips = sns.load_dataset("tips")
tips.loc[tips.time == "Lunch", "total_bill"] *= 2
def multihist(x, hue, n_bins=10, color=None, **kws):
bins = np.linspace(x.min(), x.max(), n_bins)
for _, x_i in x.groupby(hue):
plt.hist(x_i, bins, **kws)
g = sns.FacetGrid(tips, row="time", sharex=False)
g.map(multihist, "total_bill", "smoker", alpha=.5, edgecolor="w")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7203 次 |
最近记录: |