如何用美元标志和十进制格式化bar_label
我需要在海洋小号上方显示数据。数字表示美元,我想将值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
line:
i.bar_label(axes[1,0].containers["$",0, "K"])
该错误是。这可能吗?
I need to display the data above a seaborn barplot. The numbers represent dollars and I would like to display the value 25200
as $25.2k
.
This is the code I currently use to display the below graph.
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])
Which displays this:
I tried adding the following to the bar_label
line:
i.bar_label(axes[1,0].containers["quot;,0, "K"])
That error'd out however. Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您仅需要添加
$
前缀和K
后缀,则fmt
参数最简单:但是在这个在这种情况下,您还需要将值除以 1000。这对于
fmt
来说太复杂,因此可以通过labels< 将 f 字符串应用于容器的
datavalues
/代码>参数:If you only need to add a
$
prefix andK
suffix, thefmt
param is simplest:But in this case, you also need to divide the values by 1000. That's too complex for
fmt
, so instead apply an f-string to the container'sdatavalues
via thelabels
param: