在 plotly 中绘制水平线
Plot horizontal lines in plotly
我正在尝试使用 plotly 将水平线叠加到烛台图表上。但是,当我无法让水平线从 df['date'][level[0]]
中的 'x' 坐标开始时。相反,水平线开始于 x=0.
以下是我到目前为止设法收集的内容,我认为这对有经验的人来说并不漂亮。
但是,如果有任何帮助指点,我们将不胜感激:
import plotly.graph_objects as go
from plotly.offline import plot
import datetime as dt
from pandas import DataFrame
import numpy as np
import pandas as pd
hovertext=[]
for i in range(len(df['bidopen'])):
hovertext.append('Datetime: '+str(df['date'][i])+'<br>Open: '+str(df['bidopen'][i])+'<br>Close: '+str(df['bidclose'][i]))
fig = go.Figure(data=go.Candlestick(x=df['date'],
open=df['bidopen'],
high=df['bidhigh'],
low=df['bidlow'],
close=df['bidclose'],
text=hovertext,
hoverinfo='text'))
for level in levels:
fig.add_hline(level[1], df['date'][level[0]], exclude_empty_subplots=True)
plot(fig)
这是我的 plotly 代码当前生成的内容(当前输出)的屏幕截图:
下面是我在水平线(期望输出)方面想要实现的目标:
..这第二张截图代表了我能够用 matplotlib 制作的内容
由于hline()
将绘图向两侧无限延伸,您可以使用add_shape()
方法。
考虑像这样的数据框:
df = pd.DataFrame(data = {'levels':np.random.random(10)*10,
'dates':pd.date_range(start = '2020-11-01', periods = 10)})
df
levels dates
0 9.149079 2020-11-01
1 0.294123 2020-11-02
2 5.401443 2020-11-03
3 4.043063 2020-11-04
4 8.878100 2020-11-05
5 5.275614 2020-11-06
6 4.425696 2020-11-07
7 8.228720 2020-11-08
8 7.632410 2020-11-09
9 7.045191 2020-11-10
& 一个数字已经有你想要的日期作为索引:
您可以这样做:
for row in df.iterrows():
fig.add_shape(type="line", x0= row[1]['dates'],
y0 = row[1]['levels'],
x1 = df.dates.max(),
y1 = row[1]['levels'], fillcolor = 'yellow')
fig
这导致:
使用来自@Hamza 的示例,请参阅下面的解决方案:
df1 = pd.DataFrame(levels, columns = ['dates', 'levels'])
df1.set_index('dates', inplace=True)
for i in df1:
df1['dates'] = df.date[df.index.isin(df1.index)]
hovertext=[]
for i in range(len(df['bidopen'])):
hovertext.append('Datetime: '+str(df['date'][i])+'<br>Open: '+str(df['bidopen'][i])+'<br>Close: '+str(df['bidclose'][i]))
fig = go.Figure(data=go.Candlestick(x=df['date'],
open=df['bidopen'],
high=df['bidhigh'],
low=df['bidlow'],
close=df['bidclose'],
text=hovertext,
hoverinfo='text'))
for row in df1.iterrows():
fig.add_shape(type="line", x0= row[1]['dates'],
y0 = row[1]['levels'],
x1 = df.date.max(),
y1 = row[1]['levels'], fillcolor = 'yellow')
plot(fig)
我正在尝试使用 plotly 将水平线叠加到烛台图表上。但是,当我无法让水平线从 df['date'][level[0]]
中的 'x' 坐标开始时。相反,水平线开始于 x=0.
以下是我到目前为止设法收集的内容,我认为这对有经验的人来说并不漂亮。
但是,如果有任何帮助指点,我们将不胜感激:
import plotly.graph_objects as go
from plotly.offline import plot
import datetime as dt
from pandas import DataFrame
import numpy as np
import pandas as pd
hovertext=[]
for i in range(len(df['bidopen'])):
hovertext.append('Datetime: '+str(df['date'][i])+'<br>Open: '+str(df['bidopen'][i])+'<br>Close: '+str(df['bidclose'][i]))
fig = go.Figure(data=go.Candlestick(x=df['date'],
open=df['bidopen'],
high=df['bidhigh'],
low=df['bidlow'],
close=df['bidclose'],
text=hovertext,
hoverinfo='text'))
for level in levels:
fig.add_hline(level[1], df['date'][level[0]], exclude_empty_subplots=True)
plot(fig)
这是我的 plotly 代码当前生成的内容(当前输出)的屏幕截图:
下面是我在水平线(期望输出)方面想要实现的目标:
..这第二张截图代表了我能够用 matplotlib 制作的内容
由于hline()
将绘图向两侧无限延伸,您可以使用add_shape()
方法。
考虑像这样的数据框:
df = pd.DataFrame(data = {'levels':np.random.random(10)*10,
'dates':pd.date_range(start = '2020-11-01', periods = 10)})
df
levels dates
0 9.149079 2020-11-01
1 0.294123 2020-11-02
2 5.401443 2020-11-03
3 4.043063 2020-11-04
4 8.878100 2020-11-05
5 5.275614 2020-11-06
6 4.425696 2020-11-07
7 8.228720 2020-11-08
8 7.632410 2020-11-09
9 7.045191 2020-11-10
& 一个数字已经有你想要的日期作为索引:
您可以这样做:
for row in df.iterrows():
fig.add_shape(type="line", x0= row[1]['dates'],
y0 = row[1]['levels'],
x1 = df.dates.max(),
y1 = row[1]['levels'], fillcolor = 'yellow')
fig
这导致:
使用来自@Hamza 的示例,请参阅下面的解决方案:
df1 = pd.DataFrame(levels, columns = ['dates', 'levels'])
df1.set_index('dates', inplace=True)
for i in df1:
df1['dates'] = df.date[df.index.isin(df1.index)]
hovertext=[]
for i in range(len(df['bidopen'])):
hovertext.append('Datetime: '+str(df['date'][i])+'<br>Open: '+str(df['bidopen'][i])+'<br>Close: '+str(df['bidclose'][i]))
fig = go.Figure(data=go.Candlestick(x=df['date'],
open=df['bidopen'],
high=df['bidhigh'],
low=df['bidlow'],
close=df['bidclose'],
text=hovertext,
hoverinfo='text'))
for row in df1.iterrows():
fig.add_shape(type="line", x0= row[1]['dates'],
y0 = row[1]['levels'],
x1 = df.date.max(),
y1 = row[1]['levels'], fillcolor = 'yellow')
plot(fig)