【发布时间】:2021-11-22 23:02:05
【问题描述】:
在最近的 matpolotlib 更新中,我喜欢 ax.bar_label 的易用性。
我很想在最终图中隐藏低值数据标签以提高可读性,以避免标签重叠。
如何在下面的代码中隐藏小于预定义值(在这里,假设小于 0.025)的标签?
df_plot = pd.crosstab(df['Yr_Lvl_Cd'], df['Achievement_Cd'], normalize='index')
ax = df_plot.plot(kind = 'bar', stacked = True, figsize= (10,12))
for c in ax.containers:
ax.bar_label(c, label_type='center', color = "white")
【问题讨论】:
-
您可以使用this post的“原始”部分并将测试
if height > 0更新为if height > min_value。
标签: python matplotlib plot