Python,Dash,使用Pandas Dataframe导入CSV和图形生成

时间:2019-09-19 01:32:39

标签: python pandas plotly plotly-dash

我一直在想办法解决我与Python Dash有关的问题。我正在尝试执行一个简单的操作:导入CSV文件,使用数据创建散点图,并使用下拉菜单选择X和Y数据(通过数据框列名称)。一切工作正常,但图形输出仅显示与数据框中的第一个索引关联的数据。任何帮助将不胜感激。下面的代码和下面的屏幕截图: Dataframe head Output of Dash App

import base64
import datetime
import io

import dash
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html

import pandas as pd
import numpy as np

import chart_studio.plotly as py
import plotly.graph_objects as go
import plotly.io as pio
import plotly.express as px
pio.templates.default = "plotly_white"

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

df=  pd.read_csv(r'C:\Users\ajinn\Desktop\stock.csv')

app = dash.Dash()

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

app.layout= html.Div(children=[

        html.H1(children='Data Table Charter',style={'textAlign': 'center'}),
        html.Div(children='Data Charting Tool for CSV Files',style={'textAlign': 'center'}),

        html.Div([
            html.Label("Select X Series Data"),                    
            dcc.Dropdown(id='x-data',
                         options=[{'label': i, 'value': i} for i in list(df)],
                         multi=False
            )
            ],
            style={'display':'inline-block','padding-left': '0px'}
            ), 
        html.Div([
            html.Label("Select Y Series Data"),                    
            dcc.Dropdown(id='y-data',
                         options=[{'label': i, 'value': i} for i in list(df)],
                         multi=True
            )
            ],
            style={'display':'inline-block','padding-left': '100px'}
        ),

        html.Div(id='graph-data')          
])



@app.callback(
    Output(component_id='graph-data',component_property='children'),
    [Input(component_id='x-data',component_property='value'),
     Input(component_id='y-data',component_property='value')    
    ]    
)


def update_graph(xdata,ydata):
    return dcc.Graph(
               figure=go.Figure(
                   data=go.Scatter(x=df[xdata],y=df[ydata],mode='lines+markers')
               )
    )

if __name__ == '__main__':
    app.run_server(port=3337)


0 个答案:

没有答案