python - 根据时间序列 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,...]
我读过数据(在文件中就像
因为它来自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 的格式。
我该怎么做才能使时间和日期在情节中可读?
解决方案
- 直接绘制 DataFrame,不要将列提取到数组中。
- 请参阅在 pandas 中使用索引来绘制数据
df.plot()
根据索引绘制所有其他数字列。df.plot(y=['col_name'])
如果您不想绘制所有其他列,则直接指定多个列之一。
- 这使用
pandas.DataFrame.plot
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()
推荐阅读
- python - Python Pillow:putpixel() 没有正确记录颜色
- reactjs - 如何处理多个输入以创建嵌套状态属性
- pagination - Power Bi 报表生成器 - 在分页报表上添加动态图像
- python - 类外的属性函数
- python - 搜索鼠标所在十六进制网格上哪个元素的有效方法
- google-chrome - 无法让带音频的视频在空白窗口上自动播放
- database-design - 推进佣金数据库设计
- android - Android Jetpack Compose:听用户的键盘输入不使用 Enter 键
- javascript - 如何使用 JavaScript 使用 Google reCaptcha div?
- go - bun:go: 如何更改 bun 中的 `belongs-to` 关系映射字段