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"
))
添加回答
舉報