1 回答
TA贡献1853条经验 获得超6个赞
通常,texttemplate和hovertemplate可以访问跟踪级别的任何属性,即go.Bar()本例中的属性。因此x、y、 和customdata是可访问的:
import plotly.graph_objects as go
go.Figure(go.Bar(
x=["a","b"], y=[1,2], customdata=[["hi", "there"], ["hello", "there"]],
hovertemplate="x is %{x}, y is %{y}, custom1 is %{customdata[1]}",
texttemplate="x is %{x}, y is %{y}, custom1 is %{customdata[1]}",
textposition="auto"
))
customdata这里可以是列表的列表(如上所述)或字典的列表,如下访问:
import plotly.graph_objects as go
go.Figure(go.Bar(
x=["a","b"], y=[1,2], customdata=[dict(hi="there"), dict(hi="here")],
hovertemplate="x is %{x}, y is %{y}, custom1 is %{customdata.hi}",
texttemplate="x is %{x}, y is %{y}, custom1 is %{customdata.hi}",
textposition="auto"
))
该customdata格式与 Pandas 的格式兼容to_dict('records'):
import plotly.graph_objects as go
import pandas as pd
df = pd.DataFrame(dict(
x=["a","b"], y=[1,2], hi=["there", "here"]
))
go.Figure(go.Bar(
x=df.x, y=df.y, customdata=df[["hi"]].to_dict('records'),
hovertemplate="x is %{x}, y is %{y}, custom1 is %{customdata.hi}",
texttemplate="x is %{x}, y is %{y}, custom1 is %{customdata.hi}",
textposition="auto"
))
添加回答
举报