plotly:难以在绘图注释中显示带有两个美元符号的字符串

plotly : Difficulty in displaying a string with two dollar signs in plot annotation

我有一个带有两个美元符号的字符串,我想在我的绘图下方用作文本注释。 此代码创建了一个字符串,您可以看到它的输出。

str=f"Maximum: {a} , ${numerize.numerize(b)}\t"+f"Minimum: {c} , ${numerize.numerize(d)}"

Output: Maximum: 1396 , 4.41M Minimum: 1399 , 5.31M

但是当我在下面使用 str 时,会巧妙地更改字体并显示意外字符:

annotations = []
annotations.append(dict(xref='paper', yref='paper', x=0.5, y=-0.15,
                              xanchor='center', yanchor='top',
                              text=str,
                              font=dict(family="Courier New",
                                        size=14,
                                        color='rgb(100,100,100)'),
                              showarrow=False))
fig.update_layout(annotations=annotations)

和图中的结果: unexpected annotation

我该如何解决这个问题?谢谢

  • 需要在变成 HTML
  • 的文本中使用 HTML 个可打印字符
  • 因此解决方案变为
import plotly.graph_objects as go
from numerize import numerize

fig = go.Figure()

# # Output: Maximum: 1396 , 4.41M Minimum: 1399 , 5.31M
a = 1396
b = 544.41 * 10 ** 6
c = 1399
d = 255.31 * 10 ** 6
str = (
    f"Maximum: {a} , ${numerize.numerize(b)}\t"
    + f"Minimum: {c} , ${numerize.numerize(d)}"
)
ano
annotations = []
annotations.append(
    dict(
        xref="paper",
        yref="paper",
        x=0.5,
        y=-0.15,
        xanchor="center",
        yanchor="top",
        text=str,
        font=dict(family="Courier New", size=14, color="rgb(100,100,100)"),
        showarrow=False,
    )
)
fig.update_layout(annotations=annotations)