how to add different color hlines in go.figure add_hline() function previous pyplot function hlines had colors section for adding different colors for pyplot how could i do the same ?
# highest_swing and lowest_swings generate the area for which we have to check ratios
highest_swing = -1
lowest_swing = -1
for i in range(1,df.shape[0]-1):
if df['High'][i] > df['High'][i-1] and df['High'][i] > df['High'][i+1] and (highest_swing == -1 or df['High'][i] > df['High'][highest_swing]):
highest_swing = i
if df['Low'][i] < df['Low'][i-1] and df['Low'][i] < df['Low'][i+1] and (lowest_swing == -1 or df['Low'][i] < df['Low'][lowest_swing]):
lowest_swing = i
ratios = [0,0.236, 0.382, 0.5 , 0.618, 0.786,1]
colors = ["black","r","g","b","cyan","magenta","yellow"]
levels = []
max_level = df['High'][highest_swing]
min_level = df['Low'][lowest_swing]
for ratio in ratios:
if highest_swing > lowest_swing: # Uptrend
levels.append(max_level - (max_level-min_level)*ratio)
else: # Downtrend
levels.append(min_level + (max_level-min_level)*ratio)
# candlestick plot
fig = go.Figure()
fig.add_traces(go.Candlestick(x=df['Date'],
open=df['Open'],
high=df['High'],
low=df['Low'],
close=df['Close']))
start_date = df['Date'][df.index[min(highest_swing,lowest_swing)]]
end_date = df['Date'][df.index[max(highest_swing,lowest_swing)]]
y=np.array([start_date,end_date])
print(y)
for i in range(len(levels)):
# previous pyplot plot
# plt.hlines(levels[i],start_date, end_date,label="{:.1f}%".format(ratios[i]*100),colors=colors[i], linestyles="dashed")
fig.add_hline(go.Scatter(x=levels,y=y, line_shape='linear'))
fig.show()
You can try the following for a horizontal line segment:
You'll also want to change your list to colors to
colors = ["black","red","green","blue","cyan","magenta","yellow"]
since Plotly won't understand the abbreviations for"r", "g", "b"
likematplotlib.pyplot
does.Using some sample data: