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

2023-12-05

我搜索了许多制作以刻度线为中心的直方图的方法,但无法找到适用于 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))


我还想绘制一个采用单个值的变量的直方图,并选择结果直方图的箱宽度,使其以该值为中心。 (本例中为 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,然后 x 轴将绘制在负值上,即使没有并且不想显示它们。


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

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))

Bounded bars

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

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

Centered Ticks - Paired

对于您的单值图,想法保持不变。控制 bin_width 以及 x 轴范围和刻度。必须明确控制 bin 宽度,因为 bin 宽度的自动推断可能是 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'))

Output:

Single-value chart

从你的描述中我觉得条形图真正暗示的是绝对的条状图。然后自动居中。因为条不再是一个范围,而是一个离散的类别。对于示例数据中变量的数字和连续性质,我不推荐这种方法。 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()

这产生:

pandas categorical plot

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用seaborn displot 将直方图条形围绕刻度线居中?堆叠酒吧是必不可少的 的相关文章

随机推荐