Dash中图形的数据类型是什么?
大家好,我有一个CSV文件,我想用Dash来绘制里面的数据。这个CSV文件的内容是这样的:
141954,23.750
14200,23.750
14206,23.750
142012,23.750
142018,23.750
142023,23.750
我已经把每一行的数据放到了不同的列表里。现在我想把这些数据画出来。但是当我运行我的代码时,除了没有显示图表以外,其他都正常。
我找到了一些关于图表数据类型的信息。请问数据应该是数组还是字典呢?我还不太确定。
我试过把数据换成数组或者直接用列表。两个列表里的值都是正确的。
这是我的代码:
from dash import Dash, html, dcc
from os.path import isfile, join
from dash.dependencies import Input, Output
import os
mypath = '/Users/theo/Documents/Privat/Arbeiten/HiWi-Stelle/Modul-Vorbereitung/TCPServer/'
onlyfiles = [f for f in os.listdir(mypath) if isfile(join(mypath, f))]
onlyfiles.remove("TCPServer.py")
onlyfiles.remove(".DS_Store")
formatted_files = []
for file in onlyfiles:
year = file[0:4]
month = file[4:6]
day = file[6:8]
formatted_files.append(year + "." + month + "." + day)
app = Dash(__name__)
app.layout = html.Div([
html.Div(children='Temperature Plot'),
dcc.Dropdown(id='file-dropdown', options=formatted_files, placeholder="Select a file"),
dcc.Graph(id='graph')
])
@app.callback(
Output('graph', 'figure'),
[Input('file-dropdown', 'value')]
)
def update_graph(selected_file):
row1_list = []
row2_list = []
if selected_file:
file = str(selected_file).replace(".", "") + ".csv"
file_path = os.path.join(mypath, file)
with open(file_path, 'r') as f:
for line in f:
if line != "\n":
split = line.split(",")
row1_list.append(split[0])
row2_list.append(split[1])
col1_cleaned = [element.strip() for element in row1_list]
col2_cleaned = [element.strip() for element in row2_list]
integer_col1 = [int(x) for x in col1_cleaned]
float_coli2 = [float(x) for x in col2_cleaned]
fig = {
'data': [{'x': integer_col1, 'y': float_coli2, 'type': 'bar'}],
'layout': {'title': 'Graph of ' + selected_file}
}
return fig
else:
return {}
if __name__ == '__main__':
app.run(debug=True)
请原谅我的代码风格。我不太常用Python。
0 个回答
暂无回答