从 Matplotlib 图表中删除特定值

问题描述 投票:0回答:1

我不想显示图表中的 0 值,但我不想从列中删除,只是我希望它变得不可见,但此代码显示 0 值


percentage_ks = (km_50['Athlete club'].value_counts() / len(km_50)) * 100
top_12 = percentage_ks.nlargest(12)

top_12_new = top_12[top_12 != 0]

plt.figure(figsize=(10, 6))  
sns.barplot(x=top_12_new.index, y=top_12_new.values)

for index, value in enumerate(top_12_new):
    plt.text(index, value + 0.5, f'{value:.2f}%', ha='center')
    
plt.xlabel('Scientific Name')
plt.ylabel('Percentage')
plt.title('Percentage Distribution of Top 15 Scientific Names')
plt.xticks(rotation=45, ha='right')  

plt.tight_layout()
plt.show()


在此输入图片描述

在此输入图片描述

python-3.x pandas database matplotlib seaborn
1个回答
0
投票

IIUC,您想按标签而不是值过滤条形,因此您应该将

top_12_new = top_12[top_12 != 0]
替换为:

top_12_new = top_12.drop(0, errors='ignore')

输出示例:

enter image description here

使用的输入:

km_50 = pd.DataFrame({'Athlete club': ([0]*50+list(map(chr, range(65, 65+20))))*100})
© www.soinside.com 2019 - 2024. All rights reserved.