plotly 表达 : How to add comma separators for thousands on data labels

标签 plotly plotly-dash plotly-python

我正在使用 Plotly Express 中的 px.bar() 函数来创建一些简单的条形图。

我的代码如下:

import plotly.express as px
import pandas as pd

test_df = pd.DataFrame({'Manufacturer':['Ford', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW', 'Ford', 'Mercedes', 'BMW'],
                          'Metric':['Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Orders', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Sales', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty', 'Warranty'],
                          'Sector':['Germany', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA', 'Germany', 'Germany', 'Germany', 'USA', 'USA', 'USA'],
                          'Value':[45000, 70000, 90000, 65000, 40000, 65000, 63000, 2700, 4400, 3400, 3000, 4700, 5700, 1500, 2000, 2500, 1300, 2000, 2450],
                          'City': ['Frankfurt', 'Bremen', 'Berlin', 'Hamburg', 'New York', 'Chicago', 'Los Angeles', 'Dresden', 'Munich', 'Cologne', 'Miami', 'Atlanta', 'Phoenix', 'Nuremberg', 'Dusseldorf', 'Leipzig', 'Houston', 'San Diego', 'San Francisco']
                       })

df1 = pd.DataFrame(test_df.groupby(by=['Manufacturer', 'Sector'])['Value'].sum())

df1.reset_index(inplace=True)

fig = px.bar(df1, x='Manufacturer', y='Value', color='Sector', barmode='group', text='Value')

fig.show()

是否可以为条形图上的千位值添加逗号分隔符?例如,“70,900”而不是“70900”?

我查看了 https://plotly.com/python-api-reference/generated/plotly.express.bar.html#plotly.express.bar 上的文档,但没有看到任何可以控制我正在寻找的行为的东西。

提前致谢!

最佳答案

有一个带有updata_traces 功能的格式化程序,texttemplate关键字,在 D-3 format 之后.

语法为:

fig.update_traces(texttemplate='%{text:,}')

关于 plotly 表达 : How to add comma separators for thousands on data labels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62361801/

相关文章:

python - 删除 Plotly Express 中自定义悬浮卡右侧的颜色

javascript - Python点击事件的Plotly(离线)

python - Plotly Dash 无法动态创建图形

python - 如何在 plotly python 中将箱形图中位数与多类别 x 轴连接起来

python - Plotly 图形对象中线条的离散色阶 (Python)

python - 将应用程序部署到 heroku 后,一些破折号图形停止显示

r - 单击 R Shiny 中绘图图例中的名称时发生的事件

r - plotly 地嵌入到 Shiny 和 RMarkdown 中

python - 日期绘图似乎在破折号绘图框架中被破坏

javascript - 将绘图中的数据保存到 csv 或 excel 文件中