Plotly: How to use both dollar signs and linebreaks in annotations?(Plotly:如何在注释中同时使用美元符号和换行符?)
问题描述
不确定我是否在这里遗漏了一些明显的东西,但是当我在带有注释的文本中插入中断 (<br>) 时,似乎忽略它.我试过 fig.add_annotations
但同样的事情发生了.
你知道为什么这不起作用吗?
将 pandas 导入为 pd导入 plotly.graph_objects将 numpy 导入为 npdf = pd.DataFrame({"Growth_Type": ["Growing Fast", "Growing", "Stable", "Dropping", "Dropping Fast"],帐户":[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"],方向=h";))fig.update_layout(font = dict(size = 12, color = "#A6ACAF"),xaxis_tickprefix = "$",plot_bgcolor =白色",barmode =堆栈",边距 = 字典(l = 150,r = 250,b = 100,t = 100),annotations = [dict(text = 'Dropping 提供了 6.5 美元的收益分享机会和 34.4 美元的保持分享机会',外部参照 = x",yref =y",x = 360,y =下降",showarrow = 假,yanchor =底部")])图.show()
这里的问题不是换行符;它是美元符号.但是您可以使用
完整代码:
将 pandas 导入为 pd导入 plotly.graph_objects将 numpy 导入为 npdf = pd.DataFrame({"Growth_Type": ["Growing Fast", "Growing", "Stable", "Dropping", "Dropping Fast"],帐户":[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"],方向=h";))fig.update_layout(font = dict(size = 12, color = "#A6ACAF"),xaxis_tickprefix = "$",plot_bgcolor =白色",barmode =堆栈",边距 = 字典(l = 150,r = 250,b = 100,t = 100),annotations = [dict(text = 'Dropping 提供'+'$'+ '6.5 的机会和一个<br>保持'+'$'+ 的机会分享机会'34.4 磨',#annotations = [dict(text = '$',外部参照 = x",yref =y",x = 360,y =下降",showarrow = 假,yanchor =底部")])图.show()
Not sure if I'm missing something obvious here but when I'm inserting a break (<br>) into my text with annotations it just seems to ignore it. I've tried fig.add_annotations
but the same thing happens.
Do you know why this isn't working?
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()
It's not the linebreaks that are causing the trouble here; it is the dollar sign.
But you can use the printable ASCII character '$'
to get what you want instead:
text = 'Dropping presents a gain share<br>opportunity of '+ '$'+ '6.5 mill and a<br>keep share opportunity of ' + '$'+ '34.4 mill'
Plot:
Complete code:
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',
#annotations = [dict(text = '$',
xref = "x",
yref = "y",
x = 360,
y = "Dropping",
showarrow = False,
yanchor = "bottom")]
)
fig.show()
这篇关于Plotly:如何在注释中同时使用美元符号和换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Plotly:如何在注释中同时使用美元符号和换行符?
基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01