如何在同一图形的不同轨迹上添加 hover_data
How to add hover_data on different traces of same graph
fig = go.Figure()
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[0]["ToxPi Score"], name="Suffolk")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[1]["ToxPi Score"], name="Plymouth")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[2]["ToxPi Score"], name="Bristol")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[3]["ToxPi Score"], name="Norfolk")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[4]["ToxPi Score"], name="Middlesex")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[5]["ToxPi Score"], name="Worcester")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[6]["ToxPi Score"], name="Barnstable")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[7]["ToxPi Score"], name="Franklin")
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=1, label="1m", step="month", stepmode="backward"),
dict(count=6, label="6m", step="month", stepmode="backward"),
dict(count=1, label="YTD", step="year", stepmode="todate"),
dict(count=1, label="1y", step="year", stepmode="backward"),
dict(step="all")
])
)
)
fig.show()
输出:-
我使用不同的数据帧在同一图表上添加了不同的轨迹。但是当我将鼠标悬停在每一行时,它只显示 Y 的值。
我的问题是当我将鼠标悬停在轨迹上时如何添加更多数据?
P.s 我对每条轨迹使用不同的 csv
以三个股价数据为样本,使用指定y-axis值以外的数据,有自定义数据功能。在这种情况下,指定了一个数据框,并将显示模板格式组合为 html 格式。由于我们将收盘价指定为 y-axis,因此我们使用其他初始价、最高价、最低价和成交量作为悬停数据。
import yfinance as yf
import plotly.graph_objects as go
import pandas as pd
aapl = yf.download("AAPL", start="2021-01-01", end="2022-04-01")
tsla = yf.download("TSLA", start="2021-01-01", end="2022-04-01")
goog = yf.download("GOOG", start="2021-01-01", end="2022-04-01")
fig = go.Figure()
common_template = ('Open: %{customdata[0]:.2f}<br>' +
'High: %{customdata[1]:.2f}<br>' +
'Low: %{customdata[2]:.2f}<br>' +
'Adj Close: %{customdata[4]:.2f}<br>' +
'Volume: %{customdata[5]}')
fig.add_trace(
go.Scatter(x=aapl.index, y=aapl['Close'],
name="AAPL", customdata=aapl,
hovertemplate=common_template
)
)
fig.add_trace(
go.Scatter(x=tsla.index, y=tsla['Close'],
name="TSLA", customdata=tsla,
hovertemplate=common_template
)
)
fig.add_trace(
go.Scatter(x=goog.index, y=goog['Close'],
name="GOOG", customdata=goog,
hovertemplate=common_template
)
)
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=1, label="1m", step="month", stepmode="backward"),
dict(count=6, label="6m", step="month", stepmode="backward"),
dict(count=1, label="YTD", step="year", stepmode="todate"),
dict(count=1, label="1y", step="year", stepmode="backward"),
dict(step="all")
])
)
)
fig.show()
fig = go.Figure()
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[0]["ToxPi Score"], name="Suffolk")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[1]["ToxPi Score"], name="Plymouth")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[2]["ToxPi Score"], name="Bristol")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[3]["ToxPi Score"], name="Norfolk")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[4]["ToxPi Score"], name="Middlesex")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[5]["ToxPi Score"], name="Worcester")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[6]["ToxPi Score"], name="Barnstable")
)
fig.add_trace(
go.Scatter(x=alldates, y=countriesData[7]["ToxPi Score"], name="Franklin")
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=1, label="1m", step="month", stepmode="backward"),
dict(count=6, label="6m", step="month", stepmode="backward"),
dict(count=1, label="YTD", step="year", stepmode="todate"),
dict(count=1, label="1y", step="year", stepmode="backward"),
dict(step="all")
])
)
)
fig.show()
输出:-
我使用不同的数据帧在同一图表上添加了不同的轨迹。但是当我将鼠标悬停在每一行时,它只显示 Y 的值。 我的问题是当我将鼠标悬停在轨迹上时如何添加更多数据?
P.s 我对每条轨迹使用不同的 csv
以三个股价数据为样本,使用指定y-axis值以外的数据,有自定义数据功能。在这种情况下,指定了一个数据框,并将显示模板格式组合为 html 格式。由于我们将收盘价指定为 y-axis,因此我们使用其他初始价、最高价、最低价和成交量作为悬停数据。
import yfinance as yf
import plotly.graph_objects as go
import pandas as pd
aapl = yf.download("AAPL", start="2021-01-01", end="2022-04-01")
tsla = yf.download("TSLA", start="2021-01-01", end="2022-04-01")
goog = yf.download("GOOG", start="2021-01-01", end="2022-04-01")
fig = go.Figure()
common_template = ('Open: %{customdata[0]:.2f}<br>' +
'High: %{customdata[1]:.2f}<br>' +
'Low: %{customdata[2]:.2f}<br>' +
'Adj Close: %{customdata[4]:.2f}<br>' +
'Volume: %{customdata[5]}')
fig.add_trace(
go.Scatter(x=aapl.index, y=aapl['Close'],
name="AAPL", customdata=aapl,
hovertemplate=common_template
)
)
fig.add_trace(
go.Scatter(x=tsla.index, y=tsla['Close'],
name="TSLA", customdata=tsla,
hovertemplate=common_template
)
)
fig.add_trace(
go.Scatter(x=goog.index, y=goog['Close'],
name="GOOG", customdata=goog,
hovertemplate=common_template
)
)
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=1, label="1m", step="month", stepmode="backward"),
dict(count=6, label="6m", step="month", stepmode="backward"),
dict(count=1, label="YTD", step="year", stepmode="todate"),
dict(count=1, label="1y", step="year", stepmode="backward"),
dict(step="all")
])
)
)
fig.show()