gpt4 book ai didi

python - 时间戳传递给 matplotlib.date2num : 'str' object has no attribute 'toordinal'

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:49 27 4
gpt4 key购买 nike

有一个包含从文本文件中收集的时间戳(格式 %Y-%M-%D %H:%M:%S)的数组。我想用 matplotlib 在子图中绘制这些。但我无法让它工作。我在想这个:

import numpy as np
import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as md

dateconv = lambda s: datetime.strptime(s, '%Y-%M-%D %H:%M:%S:.%f')

col_names = ["timestamp", "light", "sensor1", "sensor2", "sensor3", "temp"]
dtypes = ["object", "uint8", "uint8", "uint8", "uint8", "float"]
mydata = np.genfromtxt("data.csv", delimiter=",", names = col_names, dtype=dtypes, converters={"Time": dateconv})


time = md.date2num(mydata['timestamp'])
sensor1 = mydata['sensor1']
sensor2 = mydata['sensor2']
sensor3 = mydata['sensor3']
light = mydata['light']
temp = mydata['temp']

fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')

ax1 = fig.add_subplot(3,2,1, axisbg='grey')
ax1.plot_date(time, sensor1, 'c', linewidth=2)
ax1.tick_params(axis='x', colors='c')
ax1.tick_params(axis='y', colors='c')
ax1.spines['bottom'].set_color('w')
ax1.spines['top'].set_color('w')
ax1.spines['left'].set_color('w')
ax1.spines['right'].set_color('w')
ax1.yaxis.label.set_color('c')
ax1.xaxis.label.set_color('c')
ax1.set_title('Sensor 1', color = 'c')
ax1.set_xlabel('Time')
ax1.set_ylabel('Value')
ax1.set_ylim(0, 255)

ax2 = fig.add_subplot(3,2,2, axisbg='grey')
#so on...

plt.setp(ax1.xaxis.get_majorticklabels(), rotation = 25)
plt.show()

但它不起作用,我收到以下错误:'str' 对象在第 18 行没有属性 'toordinal'(带有 md.date2num(mydata['timestamp') 的行

)

数据样本:

2014-08-12 22:45:12.826871, 65, 244, 213, 196, 21.625
2014-08-12 22:50:14.151601, 66, 246, 208, 196, 21.312
2014-08-12 22:55:15.399692, 15, 247, 208, 196, 21.375
2014-08-12 23:00:16.717546, 15, 248, 209, 195, 21.5
2014-08-12 23:05:18.041433, 15, 249, 212, 195, 21.625
2014-08-12 23:10:19.372733, 16, 248, 216, 195, 21.687

最佳答案

首先你的格式字符串是错误的。看:http://strftime.org/

%M Minute as a zero-padded decimal number.

并且 %D 根本不存在!

其次,为什么要用.date2num ? o_0 为什么不将它们存储为普通的日期时间对象,而只是 format the ticks如你所愿?

import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime

time_format = '%Y-%m-%d %H:%M:%S.%f'

col_names = ["timestamp", "light", "sensor1", "sensor2", "sensor3", "temp"]
dtypes = ["object", "uint8", "uint8", "uint8", "uint8", "float"]
mydata = np.genfromtxt("data.csv", delimiter=",", names=col_names, dtype=dtypes)

time = [datetime.strptime(i, time_format) for i in mydata['timestamp']]
sensor1 = mydata['sensor1']

fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')

ax1 = fig.add_subplot(3, 2, 1, axisbg='grey')
ax1.plot_date(time, sensor1, 'c', linewidth=2)
ax1.tick_params(axis='x', colors='c')
ax1.tick_params(axis='y', colors='c')
ax1.spines['bottom'].set_color('w')
ax1.spines['top'].set_color('w')
ax1.spines['left'].set_color('w')
ax1.spines['right'].set_color('w')
ax1.yaxis.label.set_color('c')
ax1.xaxis.label.set_color('c')
ax1.set_title('Sensor 1', color='c')
ax1.set_xlabel('Time')
ax1.set_ylabel('Value')
ax1.set_ylim(0, 255)

ax2 = fig.add_subplot(3, 2, 2, axisbg='grey')
# so on...

plt.setp(ax1.xaxis.get_majorticklabels(), rotation=25)
plt.show()

enter image description here

关于python - 时间戳传递给 matplotlib.date2num : 'str' object has no attribute 'toordinal' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25391617/

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