I am trying overlay horizontal lines to a candlestick chart using plotly. However, when I am unable to get the horizontal lines to start at the their 'x' coordinate which is found in df['date'][level[0]]
. Instead the horizontal lines are starting a x=0.
Below is what I have managed to muster together so far, which I imagine is not pretty to an experienced eye.
However any pointers to assist would be much appreciated:
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)
Here is a screen shot of what my plotly code currently produces (current output):
Below is what what I am trying to achieve in respect to the horizontal lines (desired output):
..this second screenshot represents what I was able to produce with matplotlib
Since
hline()
extends the plots to infinity on both sides, you can useadd_shape()
method.Considering a dataframe like:
& a figure already having dates you want as indices:
You can do something like:
Which results in: