使用 plotly 编辑 hovertemplate
Edit hovertemplate with plotly
porpuse 是将数据添加到 hovertemplate。我想添加 z=y1+y2=[3,3,7]。我试了很多方法都没有用,即使它看起来和其他问题相似也不是因为这个数字是怎么做的。
import plotly.express as px
fruits = ["apples", "oranges", "bananas"]
y1=[1,2,3]
y2=[2,1,4]
fig = go.Figure(data=[
go.Bar(name = 'J1', x = fruits, y = y1,
text = y1, textposition = 'outside'),
go.Bar(name = 'J2', x = fruits, y =y2,
text = y2, textposition = 'outside')])
fig.update_traces(hovertemplate="<br>".join([
"Fruit: %{x}",
"Cuantity: %{y}",
"Porcentaje mortalidad: %{z}"
]))
fig.show()
fig.show() display
你可以复制代码自己试试,如何在z中显示一个值??
- 已使用 customdata,因为这可以在 hovertemplate
中访问
- 完整代码如下
import plotly.express as px
fruits = ["apples", "oranges", "bananas"]
y1=[1,2,3]
y2=[2,1,4]
z = [x+y for x,y in zip(y1,y2)]
fig = go.Figure(data=[
go.Bar(name = 'J1', x = fruits, y = y1, customdata=z,
text = y1, textposition = 'outside'),
go.Bar(name = 'J2', x = fruits, y =y2, customdata=z,
text = y2, textposition = 'outside')])
fig.update_traces(hovertemplate="<br>".join([
"Fruit: %{x}",
"Cuantity: %{y}",
"Porcentaje mortalidad: %{customdata}"
]))
fig.show()
porpuse 是将数据添加到 hovertemplate。我想添加 z=y1+y2=[3,3,7]。我试了很多方法都没有用,即使它看起来和其他问题相似也不是因为这个数字是怎么做的。
import plotly.express as px
fruits = ["apples", "oranges", "bananas"]
y1=[1,2,3]
y2=[2,1,4]
fig = go.Figure(data=[
go.Bar(name = 'J1', x = fruits, y = y1,
text = y1, textposition = 'outside'),
go.Bar(name = 'J2', x = fruits, y =y2,
text = y2, textposition = 'outside')])
fig.update_traces(hovertemplate="<br>".join([
"Fruit: %{x}",
"Cuantity: %{y}",
"Porcentaje mortalidad: %{z}"
]))
fig.show()
fig.show() display
你可以复制代码自己试试,如何在z中显示一个值??
- 已使用 customdata,因为这可以在 hovertemplate 中访问
- 完整代码如下
import plotly.express as px
fruits = ["apples", "oranges", "bananas"]
y1=[1,2,3]
y2=[2,1,4]
z = [x+y for x,y in zip(y1,y2)]
fig = go.Figure(data=[
go.Bar(name = 'J1', x = fruits, y = y1, customdata=z,
text = y1, textposition = 'outside'),
go.Bar(name = 'J2', x = fruits, y =y2, customdata=z,
text = y2, textposition = 'outside')])
fig.update_traces(hovertemplate="<br>".join([
"Fruit: %{x}",
"Cuantity: %{y}",
"Porcentaje mortalidad: %{customdata}"
]))
fig.show()