将日期转换为可读格式为matplotlib时发生TypeError

时间:2018-12-25 12:26:38

标签: python-3.x numpy datetime matplotlib date-conversion

我正在从CSV文件导入数据。读取为数组格式后,数据具有以下格式,即打印输出(myarray)

[['2018-12-18 10:18:18' ' 5.000000E-02' ' 4.569000E-07' ... ' 5.030000E+02' '  ' ' ']

 ['2018-12-18 10:18:28' ' 1.200000E-01' ' 4.616000E-07' ... ' 5.090000E+02' '  ' ' ']

 ['2018-12-18 10:18:38' ' 2.500000E-01' ' 4.584000E-07' ... ' 5.140000E+02' '  ' ' ']

 ...
 ['2018-12-18 16:29:31' ' 5.828500E+02' ' 1.910645E+01' ...' 3.490000E+02' '  ' ' ']

 ['2018-12-18 16:29:41' ' 5.829000E+02' ' 1.913199E+01' ...' 3.490000E+02' '  ' ' ']

 ['2018-12-18 16:29:51' ' 5.828300E+02' ' 1.910634E+01' ...' 3.490000E+02' '  ' ' ']]

然后我尝试将第一列绘制为x轴,将其余列绘制为y轴。但是matplotlib不接受第0列进行索引。因此,我尝试使用matplotlib.dates进行转换,以下是我的代码

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import numpy as np

import csv
with open('Historyrecipe-2018-12-18-10-18.csv', 'r')as f:
    mylist = list(csv.reader(f, delimiter='\t'))
myarray = np.array(mylist[1:])
print(myarray)

def bytespdate2num(fmt, encoding='utf-8'):
    strconverter = mdates.strpdate2num(fmt)
    def bytesconverter(b):
        s = b.decode(encoding)
        return strconverter(s)
    return bytesconverter

date, reactorPressure, vacuumGauge, MFC1, MFC2, MFC3, rfVoltage, rfCurrent, rfPower, rfFreq, crucTemp, vacPressure = np.loadtxt(myarray,delimiter='\t', unpack=True, converters={0:bytespdate2num('%Y-%m-%d %H:%M:%S')})
plt.plot_date(date, reactorPressure,'-')
plt.show()

我收到的错误消息是

Traceback (most recent call last):
  File "D:\p\test2.py", line 33, in <module>
    converters={0:bytespdate2num('%Y-%m-%d %H:%M:%S')})
  File "C:\Python\lib\site-packages\numpy\lib\npyio.py", line 1054, in loadtxt
    first_vals = split_line(first_line)
  File "C:\Python\lib\site-packages\numpy\lib\npyio.py", line 996, in split_line
    line = regex_comments.split(line, maxsplit=1)[0]
TypeError: cannot use a string pattern on a bytes-like object

我不明白原因。需要帮忙。让我知道您是否需要有关此案例的更多信息。提前致意..圣诞快乐!

0 个答案:

没有答案