如何使用 seaborn displot 将直方图条围绕刻度线居中?堆叠杆是必不可少的

How to center the histogram bars around tick marks using seaborn displot? Stacking bars is essential

我搜索了很多制作以刻度线为中心的直方图的方法,但无法找到适用于 seaborn displot 的解决方案。函数 displot 让我可以根据数据框中的列堆叠直方图,因此我更喜欢使用 displot 的解决方案或允许基于数据框中的列进行堆叠的解决方案,颜色编码与调色板一样。

即使在设置了刻度值之后,我也无法让条形图以刻度线为中心。

示例代码

# Center the histogram on the tick marks 
tips = sns.load_dataset('tips')
sns.displot(x="total_bill",
                hue="day", multiple = 'stack', data=tips)
plt.xticks(np.arange(0, 50, 5))


我还想绘制一个采用单个值的变量的直方图,并选择结果直方图的 bin 宽度,使其以该值为中心。 (本例中为 0.5。)

我可以通过选择与刻度线数量相等的箱数来获得中心点,但生成的条非常细。在这种情况下,我该如何增加 bin 大小,其中只有一个条形图,但想要显示所有其他可能的点。通过显示所有刻度线,条形宽度非常小。 我希望条形图在 0.5 个刻度标记处同样居中,但要使其更宽,因为它是显示计数的唯一值。 有什么解决办法吗?

tips['single'] = 0.5
sns.displot(x='single',
                hue="day", multiple = 'stack', data=tips, bins = 10)
plt.xticks(np.arange(0, 1, 0.1))

编辑: 在第二种情况下是否可以更好地控制刻度线?我不想显示四舍五入到小数点后一位,而是选择要显示的刻度线。是否可以在刻度线中只显示一个值并以它为中心?

在这种情况下,min_val 和 max_val 是否指的是变量的值,在这种情况下将为 0,然后即使有 none 并且不想显示它们。

对于你的第一个问题,你可能想弄清楚你绘制的数据的一些属性。例如数据的范围。此外,您可能需要预先选择要显示的垃圾桶数量。

tips = sns.load_dataset('tips')
min_val = tips.total_bill.min()
max_val = tips.total_bill.max()
val_width = max_val - min_val
n_bins = 10
bin_width = val_width/n_bins

sns.histplot(x="total_bill",
                hue="day", multiple = 'stack', data=tips,
                bins=n_bins, binrange=(min_val, max_val),
                palette='Paired')
plt.xlim(0, 55) # Define x-axis limits

要记住的另一件事是直方图中条形的宽度 a 标识其范围的边界。因此,在 x 轴上跨越 [2,5] 的条表示该条表示的值属于该范围。

考虑到这一点,很容易制定解决方案。假设我们想要原始条形图 - 识别每个条形图的边界,一个解决方案可能看起来像

plt.xticks(np.arange(min_val-bin_width, max_val+bin_width, bin_width))

现在,如果我们将刻度偏移半个 bin 宽度,我们将到达条形的中心。

plt.xticks(np.arange(min_val-bin_width/2, max_val+bin_width/2, bin_width))

对于您的单值图,思路保持不变。控制 bin_width 和 x 轴范围和刻度。 bin-width 必须明确控制,因为 bin-width 的自动推断可能是 1 个单位宽,在绘图上没有厚度。直方图条总是表示一个范围——即使我们只有一个值。这在以下示例和图中进行了说明。

single_val = 23.5
tips['single'] = single_val
bin_width = 4

fig, axs = plt.subplots(1, 2, sharey=True, figsize=(12,4)) # Get 2 subplots 

# Case 1 - With the single value as x-tick label on subplot 0
sns.histplot(x='single',
                hue="day", multiple = 'stack', data=tips, 
                binwidth=bin_width, binrange=(single_val-bin_width, single_val+bin_width),
                palette='rocket',
                ax=axs[0])
ticks = [single_val, single_val+bin_width] # 2 ticks - given value and given_value + width
axs[0].set(
    title='Given value as tick-label starts the bin on x-axis',
    xticks=ticks,
    xlim=(0, int(single_val*2)+bin_width)) # x-range such that bar is at middle of x-axis
axs[0].xaxis.set_major_formatter(FormatStrFormatter('%.1f'))

# Case 2 - With centering on the bin starting at single-value on subplot 1
sns.histplot(x='single',
                hue="day", multiple = 'stack', data=tips, 
                binwidth=bin_width, binrange=(single_val-bin_width, single_val+bin_width),
                palette='rocket',
                ax=axs[1])

ticks = [single_val+bin_width/2] # Just the bin center
axs[1].set(
    title='Bin centre is offset from single_value by bin_width/2',
    xticks=ticks,
    xlim=(0, int(single_val*2)+bin_width) ) # x-range such that bar is at middle of x-axis
axs[1].xaxis.set_major_formatter(FormatStrFormatter('%.1f'))

输出:

我从你的描述中感觉到你用条形图真正暗示的是一个分类条形图。然后自动对中。因为酒吧不再是一个范围,而是一个离散的类别。对于示例数据中变量的数字和连续性质,我不推荐这种方法。 Pandas 提供绘制分类条形图。参见 here。对于我们的示例,一种方法如下:

n_colors = len(tips['day'].unique()) # Get number of uniques categories
agg_df = tips[['single', 'day']].groupby(['day']).agg(
    val_count=('single', 'count'),
    val=('single','max')
).reset_index() # Get aggregated information along the categories
agg_df.pivot(columns='day', values='val_count', index='val').plot.bar(
    stacked=True,
    color=sns.color_palette("Paired", n_colors), # Choose "number of days" colors from palette
    width=0.05 # Set bar width
    ) 
plt.show()

这产生: