从下拉列表中选择数据,并使用所选数据绘出多个图表

2024-09-24 10:28:07 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我是新来的。我通过在线教程学习。我想做的是从下拉列表中选择一个数据,并使用该数据,需要创建一些图表。 数据源:有5列的Csv

设备名称、日班(包含日期和班次,例如:第10-21天、第10-21天、第10-22天等)、生产、温度和湿度

在DeviceName列中包含不同的机器名(W01、w02等)。基本上,原始数据集包含所有设备的数据。选择下拉列表,我选择了一个设备并绘制了所有其他图表(让我选择W01机器的当天,并绘制所选机器的温度和湿度(与日班相对)(通过下拉列表)

我用下拉菜单创建了它,并根据所选机器的日班绘制生产图。但我不知道如何为选定的机器创建温度和湿度图表

下面给出了我尝试的方法(这是可行的。在这里,我从下拉列表中选择了机器名称,并根据所选机器的日班绘制生产图,我要做的是在上面为所选机器创建的图表下方绘制温度和湿度图表)

#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import pandas as pd
import plotly.express as px  # (version 4.7.0)
import plotly.graph_objects as go


import dash  # (version 1.12.0) pip install dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

# -------
#-----------------------------------------------------------------------
# Import and clean data (importing csv into pandas)
import pandas as pd
df=pd.read_excel("sample.xlsx")

dfnew=df[df['Start Time']>1602460800]#data after oct 12th
dfnew["Device Name"] = dfnew["Device Name"].str.strip()#remove spaces in device name
#dfnew["Device Name"]= dfnew["Device Name"].str.replace("-","")
# ------------------------------------------------------------------------------
# App layout
app.layout = html.Div([

    html.H1("Production Analysis by Shift", style={'text-align': 'center'}),

    dcc.Dropdown(id="slct_machine",
                 options=[
                     {"label": "W01", "value": 'W01'},
                     {"label": "W02", "value": 'W02'},
                     {"label": "W02", "value": 'W03'},
                     {"label": "W04", "value": 'W04'},                   
                     {"label": "W05", "value": 'W05'},
                     {"label": "W02", "value": 'W06'},
                     {"label": "W02", "value": 'W07'},
                     {"label": "W02", "value": 'W08'},
                     {"label": "W08", "value": 'W09'}],
                 
                 multi=False,
                 value='W08',
                 style={'width': "40%"}
                 ),

    html.Div(id='output_container', children=[]),
    html.Br(),

    dcc.Graph(id='my_bee_map', figure={})

])


# ------------------------------------------------------------------------------
# Connect the Plotly graphs with Dash Components
@app.callback(
    [Output(component_id='output_container', component_property='children'),
     Output(component_id='my_bee_map', component_property='figure')],
    [Input(component_id='slct_machine', component_property='value')]
)
def update_graph(option_slctd):
    print(option_slctd)
    print(type(option_slctd))

    container = "The machine chosen by user was: {}".format(option_slctd)

    dff = dfnew
    dff = dff[dff["Device Name"] == option_slctd]
    

    fig = px.line(dff, x='Day-Shift', y='Production')

    
    #fig.update_xaxes(tickangle = 20)                            
    
    fig.update_layout(
        autosize=False,
        width=1800,
        height=800, title='Production Of Shift',template="plotly_dark")    

    # Plotly Graph Objects (GO)
    # fig = go.Figure(
    #     data=[go.Choropleth(
    #         locationmode='USA-states',
    #         locations=dff['state_code'],
    #         z=dff["Pct of Colonies Impacted"].astype(float),
    #         colorscale='Reds',
    #     )]
    # )
    #
    # fig.update_layout(
    #     title_text="Bees Affected by Mites in the USA",
    #     title_xanchor="center",
    #     title_font=dict(size=24),
    #     title_x=0.5,
    #     geo=dict(scope='usa'),
    # )

    return container, fig


# ------------------------------------------------------------------------------
if __name__ == '__main__':
    app.run_server(debug=True)


# In[ ]:

示例数据集(选择特定机器后。例如:W01的此数据帧)如下所示:

Day-Shift   Production  Humidity    Temparature
Day 11-06    512         65         25
Night 11-06  746         70         28
Day 11-07    527         45         23
Night 11-07  379         56         27
Day 11-08    605         68         26
Night 11-08  400         49         24

Tags: 数据import机器idvaluehtmlas图表