gpt4 book ai didi

python - 使用 Dash 和 Plotly 的多折线图

转载 作者:行者123 更新时间:2023-12-01 06:52:04 24 4
gpt4 key购买 nike

我正在尝试创建多个折线图,其中 X 轴上的每个“机器”和“时间”各有一行。然而我尝试了很多组合来生成 X 和 Y 标签。我得到的最接近预期结果的是下面的图表。

  • 欢迎提出任何建议。

这是数据框:

df3:
Machine Units Pruduced Time
K2K02 1900 15
K2K03 311 15
K2K01 5230 17
K2K02 1096 12
K2K03 1082 11
K2K07 587 17...

所以我正在分组和拆垛以准备 df 绘制图表。

fdr = dff3.groupby(['Time', 'Machine']).sum()
fdr2 = fdr.unstack('Time')
fdr2

这是我的代码:

import dash_table_experiments
import dash
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd
import plotly.graph_objects as go
import numpy as np
import dash_table_experiments as dash_table
import dash
from dash.dependencies import Input, Output
import dash_table
import pandas as pd
import plotly_express as px
import plotly.offline as pyo

dff3 = pd.read_csv('df3.csv', encoding='latin-1', low_memory=False)
dff3
fdr = dff3.groupby(['Time', 'Machine']).sum()
fdr2 = fdr.unstack('Time')
fdr2

app = dash.Dash()

app.layout = html.Div([

dcc.Graph(
id = 'GrapGo',
figure = {
'data' : [
go.Scatter(

x = fdr2.index,
y = fdr2.loc[rowname],
mode = "markers+lines",
name = rowname
)for rowname in fdr2.index


],
'layout' : go.Layout(
title = "Grafico com go",
xaxis = {'title': 'X Label'},
yaxis = {'title': 'y Label'}

)
}
)

])

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

这是我得到的图表:

预期结果是:

enter image description here

最佳答案

我在您的问题中没有看到需要运行服务器。如果您只需要生成一个绘图,那么此代码可以完成这项工作(Dash v1.6.0):

import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame({'Machine': ['K2K01','K2K01','K2K01','K2K02','K2K02','K2K02','K2K03','K2K03','K2K03'],
'Units': [100,200,400,400,300,100,500,700,500],
'Time':[11,12,13,11,12,13,11,12,13]})

groups = df.groupby(by='Machine')

data = []
colors=['red', 'blue', 'green']

for group, dataframe in groups:
dataframe = dataframe.sort_values(by=['Time'])
trace = go.Scatter(x=dataframe.Time.tolist(),
y=dataframe.Units.tolist(),
marker=dict(color=colors[len(data)]),
name=group)
data.append(trace)

layout = go.Layout(xaxis={'title': 'Time'},
yaxis={'title': 'Produced Units'},
margin={'l': 40, 'b': 40, 't': 50, 'r': 50},
hovermode='closest')

figure = go.Figure(data=data, layout=layout)
figure.show()

enter image description here

关于python - 使用 Dash 和 Plotly 的多折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58941305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com