Python / Dash:图中单个子图中的多个图形

时间:2018-04-28 10:28:29

标签: python plotly plotly-dash

我需要在Dash图中的一个子图中包含多个线图。

在下面的代码中有3个子图' Price' ,' MMA30'和'卷'在图中作为3个单独的子图。

我希望得到' Price'和' MMA30'在一个子情节中。

复制代码:

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)

我试图合并' Price'和' MMA30'通过将子图改为2行并将其绘制成一个子图。 tls.make_subplot

中的1列

并将fig.append_trace更改为

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)

我认为将两个线条图作为一个序列列表将会解决,但显然它没有。另外,如果没有将它们放在列表中就行了 - 没有用。

在Dash模块的其中一个子图中实现多个线图的任何方法。

谢谢。

2 个答案:

答案 0 :(得分:1)

您可以通过为// classic extend: 'Ext.form.Panel', items: [{ xtype: 'container', layout: 'vbox', items: [{ xtype: 'container', layout: 'hbox', items: [{-----}] },{ xtype: 'grid', title: 'Hours of Operation', itemId: 'hoursOfOperationPanel', store: hoursStore // don't set this to an instance },{ xtype: 'container', layout: 'hbox', items: [{-----}] }] }] // modern extend: 'Ext.form.Panel', items: [{ xtype: 'container', layout: 'vbox', items: [{ xtype: 'container', layout: 'hbox', items: [{-----}] },{ xtype: 'panel', title: 'Hours of Operation', itemId: 'hoursOfOperationPanel', layout: 'fit', items: [{ xtype: 'grid', store: hoursStore // don't set this to an instance }] },{ xtype: 'container', layout: 'hbox', items: [{-----}] }] }] 提供正确的参数来进行设置。

在以下示例中,我将行设置为2并更改了fig.append_trace()的调用:

append_trace

结果是这个图: enter image description here

答案 1 :(得分:1)

import plotly.tools as tls # is decapetaded so use this instead 
from plotly.subplots import make_subplots
fig = 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)
相关问题