我需要在 seaborn barplot 上方顯示資料。這些數字代表美元,我想將值顯示25200為$25.2k。
這是我目前用來顯示下圖的代碼。
import seaborn as sns
import matplotlib.pyplot as plt
i = sns.barplot(ax = axes[1,0], x=lr_2021['decile'], y=np.round((lr_2021['NonCAT Severity']/1000),2), color = 'purple')
i.bar_label(axes[1,0].containers[0])
顯示這個:

我嘗試將以下內容添加到該bar_label行:
i.bar_label(axes[1,0].containers["$",0, "K"])
然而,那個錯誤已經消失了。這可能嗎?
uj5u.com熱心網友回復:
如果只需要添加$前綴和K后綴,則fmt引數最簡單:
i.bar_label(axes[1,0].containers[0], fmt='$%gK')
但在這種情況下,您還需要將這些值除以 1000。這對于 來說太復雜了,因此請改為通過引數fmt將 f 字串應用于容器:datavalueslabels
bars = axes[1,0].containers[0]
i.bar_label(bars, labels=[f'${value/1000:.1f}K' for value in bars.datavalues])
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/453356.html
標籤:Python matplotlib 海运 条形图
