Sjo*_*lon 5 python timestamp matplotlib
有一个%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()
Run Code Online (Sandbox Code Playgroud)
但它不起作用我收到以下错误:
'str' object has no attribute 'toordinal'在第 18 行(带有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
Run Code Online (Sandbox Code Playgroud)
首先你的格式字符串是错误的。看: http: //strftime.org/
%M 分钟,以零填充的十进制数。
并且%D根本不存在!
其次,为什么使用.date2num?o_0 为什么不将它们存储为普通日期时间对象,而只是根据需要格式化刻度?
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()
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
18917 次 |
| 最近记录: |