I need to have multiple line plots inside one of the subplots of the figure in Dash plots.
In the below code there are 3 subplots 'Price' , 'MMA30' and 'Volume' in the figure as 3 separate subplots.
I want to have the 'Price' and 'MMA30' in one subplot.
Code for reproducing:
import dash,requests,pandas as pd
from dash.dependencies import Output, Input
import dash_core_components as dcc
import dash_html_components as html
import plotly.tools as tls
from io import StringIO
app = dash.Dash(__name__)
app.layout = html.Div([
html.Div(['Name : ',
dcc.Input(id='input',value='ACC',type='text')
]),
dcc.Graph(id='price_volume')
])
@app.callback(
Output('price_volume', 'figure'),
[Input(component_id='input', component_property = 'value')]
)
def update_graph(in_data):
p=requests.get('http://finance.google.com/finance/getprices?q='+in_data+'&x=NSE&i=61&p=1d&f=d,c,v').text
a=pd.read_csv(StringIO(p),skiprows=range(7),names =['date','Close','Volume'])
a['date']=pd.to_datetime(a.date.str[1:],unit='s').dt.tz_localize('UTC').dt.tz_convert('Asia/Kolkata')
a['Date']=a.date.dt.date
a['Time']=a.date.dt.time
df = a[['Date','Time','Close','Volume']]
df['MMA30']=df.Close.rolling(window=30).mean()
fig = tls.make_subplots(rows=3, cols=1, shared_xaxes=True,vertical_spacing=0.009,horizontal_spacing=0.009)
fig['layout']['margin'] = {'l': 30, 'r': 10, 'b': 50, 't': 25}
fig.append_trace({'x':df.Time,'y':df.Close,'type':'scatter','name':'Price'},1,1)
fig.append_trace({'x':df.Time,'y':df.MMA30,'type':'scatter','name':'MMA30'},2,1)
fig.append_trace({'x':df.Time,'y':df.Volume,'type':'bar','name':'Volume'},3,1)
fig['layout'].update(title='1 Minute plot of '+in_data)
return fig
if __name__ == '__main__':
app.run_server(debug=True)
I tried to merge the 'Price' and 'MMA30' plots into one subplot by changing no of subplots to 2 rows & 1 column in the tls.make_subplot
line
and changed the fig.append_trace
to
fig.append_trace([{'x':df.Time,'y':df.Close,'type':'scatter','name':'Price'},
{'x':df.Time,'y':df.MMA30,'type':'scatter','name':'MMA30'}],1, 1)
fig.append_trace({'x':df.Time,'y':df.Volume,'type':'bar','name':'Volume'}, 2, 1)
I thought giving the two line plots as a list of dicts will solve but apparently it did not.Also, did it without putting them in a list - didnt work.
Any way to achieve multiple line plots inside one of the subplots in Dash module.
Thank you.
You can set this up by providing the right argument to fig.append_trace()
.
In the following example i have set the rows to 2 and changed the calls of append_trace
:
fig = tls.make_subplots(rows=2, cols=1, shared_xaxes=True,vertical_spacing=0.009,horizontal_spacing=0.009)
fig['layout']['margin'] = {'l': 30, 'r': 10, 'b': 50, 't': 25}
fig.append_trace({'x':df.Time,'y':df.Close,'type':'scatter','name':'Price'},1,1)
fig.append_trace({'x':df.Time,'y':df.MMA30,'type':'scatter','name':'MMA30'},1,1)
fig.append_trace({'x':df.Time,'y':df.Volume,'type':'bar','name':'Volume'},2,1)