I have a scatter plot
where y axis is a column with stock price
can i draw a line for referencing the lower bound and upper bound
I have a scatter plot
where y axis is a column with stock price
can i draw a line for referencing the lower bound and upper bound
if fig is your scatter plot you have to add two more traces to this figure, one for the lower bound and another for the upper bound:
import pandas as pd
from datetime import datetime
import plotly.graph_objects as go
import numpy as np
df = pd.DataFrame({'date': [datetime(2019, 12, k) for k in range(1,30)],
'price': 30+15*np.random.rand(29)})
fig = go.Figure(go.Scatter(x=df['date'], y=df['price']))
fig.add_trace(go.Scatter(x=df['date'], y =[df['price'].max()]*len(df), mode='lines', line_color='red'))
fig.add_trace(go.Scatter(x=df['date'], y =[df['price'].min()]*len(df), mode='lines', line_color='green'))
is there a way to add green color between those two traces and below and above of the traces to be set to different color?
Yes, you can draw fill rectangles below your traces:
import pandas as pd
from datetime import datetime
import plotly.graph_objects as go
import numpy as np
df = pd.DataFrame({'date': [datetime(2019, 12, k) for k in range(1,30)],
'price': 30+15*np.random.rand(29)})
prmin, prmax = df['price'].min(), df['price'].max()
dtmin, dtmax = df['date'].min(), df['date'].max()
fig = go.Figure(go.Scatter(x = [dtmin, dtmax, dtmax, dtmin],
y = [prmin, prmin, prmax, prmax],
mode='lines',
fill='toself',
fillcolor='#98FB98', #'#42e360',
line_color='#98FB98',
opacity=0.7,
showlegend=False,
hoverinfo='none'))
h =1 #the vertical distance from the green area to the upper line
#respectively lower line of the plot window
llim = prmin-h
ulim = prmax+h
fig.add_trace(go.Scatter(x = [dtmin, dtmax, dtmax, dtmin],
y = [llim, llim, prmin, prmin],
mode='lines',
fill='toself',
fillcolor='#d7d7d7',
line_color='#d7d7d7',
showlegend=False
))
fig.add_trace(go.Scatter(x = [dtmin, dtmax, dtmax, dtmin],
y = [prmax, prmax, ulim, ulim],
mode='lines',
fill='toself',
fillcolor='#d7d7d7',
line_color='#d7d7d7',
showlegend=False
))
fig.add_trace(go.Scatter(x=df['date'], y=df['price'], line_color='black'))
fig.add_trace(go.Scatter(x=df['date'], y =[prmax]*len(df), mode='lines', line_color='red', line_width=2))
fig.add_trace(go.Scatter(x=df['date'], y =[prmin]*len(df), mode='lines', line_color='red', line_width=2))
fig.update_yaxes(range= [llim, ulim])
fig