python /破折号 : Multiple graphs inside a single subplot of the the figure

标签 python plotly plotly-dash

我需要在破折号图中的图形的一个子图中绘制多条线图。

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

我想在一个子图中包含“价格”和“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)

我试图通过将 tls.make_subplot 行中的子图数量更改为 2 行和 1 列来将“价格”和“MMA30”图合并为一个子图

并将 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 模块的一个子图中实现多线图的任何方法。

谢谢。

最佳答案

您可以通过向 fig.append_trace() 提供正确的参数来进行设置。

在下面的示例中,我将行设置为 2 并更改了 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)

结果是这张图: enter image description here

关于 python /破折号 : Multiple graphs inside a single subplot of the the figure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50075496/

相关文章:

python - 当我拖动无框窗口时,如何停用 Windows 任务栏上的鼠标运动 (<Motion-1>)?

plotly - 绘图中的树形图没有给出独特的颜色

python - 将修改后的破折号数据表保存到数据框

python-3.x - Dash App Table 链接不可点击 - Python Web 应用程序问题

python - Plotly 似乎丢失了热图的标签

plotly-dash - 情节破折号 : change default port

python - 我想将 pandas DataFrame 中的两列相乘并将结果添加到新列中

python - 从 Python C 扩展中导入和使用标准 Python 模块

python - 使用 for 循环创建一个 DF 时,Pandas concat 无法按预期工作

r - 设置绘图热图的 x 和 y 范围