Plotly:如何在注释中同时使用美元符号和换行符?

Flu*_*990 4 python plotly

不确定我是否在这里遗漏了一些明显的东西,但是当我在带有注释的文本中插入一个中断(<br>)时,它似乎忽略了它。我已经尝试过fig.add_annotations,但同样的事情发生了。

你知道为什么这不起作用吗?

import pandas as pd
import plotly.graph_objects as go
import numpy as np

df = pd.DataFrame({"Growth_Type": ["Growing Fast", "Growing", "Stable", "Dropping", "Dropping Fast"],
                  "Accounts": [407,1275,3785,1467,623],
                  "Gain_Share": [1.20,8.1,34.4,6.5,0.4],
                  "Keep_Share": [16.5, 101.2, 306.3, 107.2, 27.7]})

df2 = pd.concat([pd.DataFrame({"Growth_Type":df["Growth_Type"], 
              "Opportunity_Type": np.repeat("Gain Share", 5),
             "Wallet_Share": df["Gain_Share"]}),
          pd.DataFrame({"Growth_Type":df["Growth_Type"], 
              "Opportunity_Type": np.repeat("Keep Share", 5),
             "Wallet_Share": df["Keep_Share"]})])

fig = go.Figure()

fig.add_trace(go.Bar(x = df2["Wallet_Share"], 
                     y = df2["Growth_Type"],
                     orientation = "h"
                    ))

fig.update_layout(font = dict(size = 12, color = "#A6ACAF"),
                 xaxis_tickprefix = "$",
                  plot_bgcolor = "white",
                  barmode = "stack",
                  margin = dict(l = 150, 
                               r = 250,
                               b = 100,
                               t = 100),
                  annotations = [dict(text = 'Dropping presents a gain share<br>opportunity of $6.5 mill and a<br>keep share opportunity of $34.4 mill',
                  xref = "x",
                  yref = "y",
                  x = 360,
                  y = "Dropping",
                  showarrow = False,
                  yanchor = "bottom")]
                 )

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

在此输入图像描述

ves*_*and 8

造成这里麻烦的不是换行符;而是换行符。这是美元符号。但是您可以使用可打印的 ASCII 字符 '&#36;'来获取您想要的内容:

text = 'Dropping presents a gain share<br>opportunity of '+ '&#36;'+ '6.5 mill and a<br>keep share opportunity of ' + '&#36;'+ '34.4 mill'
Run Code Online (Sandbox Code Playgroud)

阴谋:

在此输入图像描述

完整代码:

import pandas as pd
import plotly.graph_objects as go
import numpy as np

df = pd.DataFrame({"Growth_Type": ["Growing Fast", "Growing", "Stable", "Dropping", "Dropping Fast"],
                  "Accounts": [407,1275,3785,1467,623],
                  "Gain_Share": [1.20,8.1,34.4,6.5,0.4],
                  "Keep_Share": [16.5, 101.2, 306.3, 107.2, 27.7]})

df2 = pd.concat([pd.DataFrame({"Growth_Type":df["Growth_Type"], 
              "Opportunity_Type": np.repeat("Gain Share", 5),
             "Wallet_Share": df["Gain_Share"]}),
          pd.DataFrame({"Growth_Type":df["Growth_Type"], 
              "Opportunity_Type": np.repeat("Keep Share", 5),
             "Wallet_Share": df["Keep_Share"]})])

fig = go.Figure()

fig.add_trace(go.Bar(x = df2["Wallet_Share"], 
                     y = df2["Growth_Type"],
                     orientation = "h"
                    ))

fig.update_layout(font = dict(size = 12, color = "#A6ACAF"),
                 xaxis_tickprefix = "$",
                  plot_bgcolor = "white",
                  barmode = "stack",
                  margin = dict(l = 150, 
                               r = 250,
                               b = 100,
                               t = 100),
                  annotations = [dict(text = 'Dropping presents a gain share<br>opportunity of '+ '&#36;'+ '6.5 mill and a<br>keep share opportunity of ' + '&#36;'+ '34.4 mill',
                  #annotations = [dict(text = '&#36;',
                  xref = "x",
                  yref = "y",
                  x = 360,
                  y = "Dropping",
                  showarrow = False,
                  yanchor = "bottom")]
                 )

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