Plotly 中的 $ 刻度格式化程序尊重负值

Max*_*nis 3 python plotly plotly-python

根据在 python 中使用plotly方法在y轴刻度中添加货币符号的答案您可以通过以下方式将货币添加到Python Plotly刻度格式化程序:

fig.update_layout(yaxis_tickprefix = '$', yaxis_tickformat = ',.')
Run Code Online (Sandbox Code Playgroud)

但是,这显示负数而$-100不是-$100

如何格式化刻度以包含 $ 并尊重负数?

r-b*_*ers 6

如果您在刻度格式中设置美元符号而不是前缀,您将获得预期的格式。

import plotly.graph_objects as go
import numpy as np

fig = go.Figure(go.Scatter(
    x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
    y = np.random.randint(-20,20,12)
))
fig.update_layout(yaxis_tickformat='$,')#yaxis_tickprefix='$',

fig.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述