亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Plotly:如何在注釋中同時使用美元符號和換行符?

Plotly:如何在注釋中同時使用美元符號和換行符?

MM們 2023-07-18 16:34:26
不確定我是否在這里遺漏了一些明顯的東西,但是當我在帶有注釋的文本中插入一個中斷(<br>)時,它似乎忽略了它。我已經嘗試過fig.add_annotations,但同樣的事情發生了。你知道為什么這不起作用嗎?import pandas as pdimport plotly.graph_objects as goimport numpy as npdf = 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()
查看完整描述

1 回答

?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

造成這里麻煩的不是換行符;而是換行符。這是美元符號。但是您可以使用可打印的 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'

陰謀:

http://img1.sycdn.imooc.com/64b64ec00001b9a806350330.jpg

完整代碼:

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()


查看完整回答
反對 回復 2023-07-18
  • 1 回答
  • 0 關注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號