首页 > 解决方案 > 根据时间序列 x 轴绘制值

问题描述

我正在使用导入到 Excel 表的 .csv 文件,其日期和时间为:

['2017-09-01 00:00:00.000' '2017-09-01 00:05:00.000'
 '2017-09-01 00:10:00.000' ... '2017-09-30 23:45:00.000'
 '2017-09-30 23:50:00.000' '2017-09-30 23:55:00.000']

和通量为:

[3.233,3.928,0,8.333,...]

我读过数据(在文件中就像

日期和时间的第 1 列,通量值的第 2 列

因为它来自excel表)

如何将其绘制为每个日期的所有时间,而不是仅绘制日期与通量?结果,我得到了一个非常减少的情节,这是不正确的。我需要 2017 年 9 月 1 日 00:00:00.000,00:10:00.000 等(所有这些都可以从我正在使用的 .csv 文件中获得,读取不正确)与每次实例中的通量。

我刚用过

col_list = ['time_tag', 'ZPGT10E', ...]
df = pd.read_csv('sep2017.csv', usecols=col_list, skiprows = 717) 
z10edt = df['time_tag'].to_numpy()

导入数据然后绘制:

x = [['2017-09-01', '00:00:00.000'], ['2017-09-01', '00:05:00.000'], ['2017-09-01', '00:10:00.000'], ...['2017-09-01', '00:15:00.000']]
plt.plot(x,avg,color='red',label='average')

但我当然得到了错误:

TypeError: unhashable type: 'numpy.ndarray'由于 x 的格式。

我该怎么做才能使时间和日期在情节中可读?

标签: pythonpandasmatplotlibplot

解决方案


import pandas as pd

# read in the data
df = pd.read_csv('sep2017.csv', usecols=col_list, skiprows=717)

# convert the time_tag column to a datetime dtype
df.time_tag = pd.to_datetime(df.time_tag)

# set the time_tag column as the index
df.set_index('time_tag', inplace=True)

# plot the dataframe
df.plot()

推荐阅读