如何在使用 figure.add_shape(type='line') 创建的 Plotly 中为水平线的 Y 值添加悬停注释
How to add Hovering Annotation for the Y value of a Horizontal Line in Plotly created using figure.add_shape(type='line')
TL;DR:如何为以下代码添加显示 5
的悬停注释:
fig.add_shape(type="line", x0=1, x1=2, y0=5, y1=5,line_width=1.5, line_dash="dot", line_color="red")
我有这段代码,用于显示基于数据在其上绘制枢轴线的烛台。它工作得很好。当您将光标移动到水平线以获取准确值时,我只想显示 y
值。下面是我用来显示水平轴、支撑阻力和垂直日间断线的代码部分:
# I already have a Figure Object from plotly which I access
fig = go.Figure(data = [go.Candlestick(opacity = 0.9, x = stocks[Date], name = 'X',
open = stocks[Open], high = stocks[High], low = stocks[Low], close = stocks[Close]),])
fig.add_shape(type="line", x0=1, x1=2, y0=5, y1=5,line_width=1.5, line_dash="dot", line_color="red")
如何向这些添加悬停值信息?
这是我的实际图表,所有 Horizontal
线都是使用 fig.add_shape(type='line')
绘制的
- 直接用走线替换布局形状
- 这在您的示例代码的范围内显示了这一点,我不得不暗示您的数据从纪元开始并且您的行只有 4 毫秒,所以我将它延长到 4.5 小时
import plotly.graph_objects as go
import pandas as pd
from datetime import datetime
import numpy as np
# setup all necessary variables and MWE could not be provide :-(
stocks = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv",
parse_dates=["Date"],
)
$ rebase data to start at epoch as sample code implies it does
stocks["Date"] = pd.date_range("1-jan-1970", freq="1Min", periods=len(stocks))
Date = "Date"
Open = "AAPL.Open"
High = "AAPL.High"
Low = "AAPL.Low"
Close = "AAPL.Close"
fig = go.Figure(
data=[
go.Candlestick(
opacity=0.9,
x=stocks[Date],
name="X",
open=stocks[Open],
high=stocks[High],
low=stocks[Low],
close=stocks[Close],
),
]
)
# replace this line of code. but it's implications are weird. line for 4 milliseconds just after epoch
# fig.add_shape(type="line", x0=1, x1=2, y0=5, y1=5,line_width=1.5, line_dash="dot", line_color="red")
# replace with this - let's make it 4.5hrs long
fig.add_shape(
type="line",
x0=1,
x1=1.5 * 10 ** 7,
y0=5,
y1=5,
line_width=1.5,
line_dash="dot",
line_color="red",
)
# equivalend as a trace, and now have hover...
fig.add_traces(
go.Scatter(
x=np.linspace(1, 1.5 * 10 ** 7, 100),
y=np.repeat([5], 100),
mode="lines",
line_dash="dot",
line_color="red",
showlegend=False,
)
)
TL;DR:如何为以下代码添加显示 5
的悬停注释:
fig.add_shape(type="line", x0=1, x1=2, y0=5, y1=5,line_width=1.5, line_dash="dot", line_color="red")
我有这段代码,用于显示基于数据在其上绘制枢轴线的烛台。它工作得很好。当您将光标移动到水平线以获取准确值时,我只想显示 y
值。下面是我用来显示水平轴、支撑阻力和垂直日间断线的代码部分:
# I already have a Figure Object from plotly which I access
fig = go.Figure(data = [go.Candlestick(opacity = 0.9, x = stocks[Date], name = 'X',
open = stocks[Open], high = stocks[High], low = stocks[Low], close = stocks[Close]),])
fig.add_shape(type="line", x0=1, x1=2, y0=5, y1=5,line_width=1.5, line_dash="dot", line_color="red")
如何向这些添加悬停值信息?
这是我的实际图表,所有 Horizontal
线都是使用 fig.add_shape(type='line')
- 直接用走线替换布局形状
- 这在您的示例代码的范围内显示了这一点,我不得不暗示您的数据从纪元开始并且您的行只有 4 毫秒,所以我将它延长到 4.5 小时
import plotly.graph_objects as go
import pandas as pd
from datetime import datetime
import numpy as np
# setup all necessary variables and MWE could not be provide :-(
stocks = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv",
parse_dates=["Date"],
)
$ rebase data to start at epoch as sample code implies it does
stocks["Date"] = pd.date_range("1-jan-1970", freq="1Min", periods=len(stocks))
Date = "Date"
Open = "AAPL.Open"
High = "AAPL.High"
Low = "AAPL.Low"
Close = "AAPL.Close"
fig = go.Figure(
data=[
go.Candlestick(
opacity=0.9,
x=stocks[Date],
name="X",
open=stocks[Open],
high=stocks[High],
low=stocks[Low],
close=stocks[Close],
),
]
)
# replace this line of code. but it's implications are weird. line for 4 milliseconds just after epoch
# fig.add_shape(type="line", x0=1, x1=2, y0=5, y1=5,line_width=1.5, line_dash="dot", line_color="red")
# replace with this - let's make it 4.5hrs long
fig.add_shape(
type="line",
x0=1,
x1=1.5 * 10 ** 7,
y0=5,
y1=5,
line_width=1.5,
line_dash="dot",
line_color="red",
)
# equivalend as a trace, and now have hover...
fig.add_traces(
go.Scatter(
x=np.linspace(1, 1.5 * 10 ** 7, 100),
y=np.repeat([5], 100),
mode="lines",
line_dash="dot",
line_color="red",
showlegend=False,
)
)