Matplotlib dateformat问题

时间:2017-02-07 15:55:27

标签: python numpy matplotlib

我正在尝试使用matplotlib使用以下代码绘制时间序列数据

import matplotlib.pyplot as plt

x, y = data.shape
y_metrics = alarms_metrics + udp_alarms_metrics_names
print x, y
for j in range(1, y):
    time_stamp = []
    metric = []
    plt.figure()
    for i in range(x):
        time_stamp.append(data_time[i][0])
        metric.append(data[i][j])
    plt.xlabel("Time")
    plt.ylabel(str(y_metrics[j-1]))
    plt.plot(time_stamp, metric)
    plt.savefig(str(j))

time_stamp的格式为

['2017:02:01:14:44:00', '2017:02:01:14:44:01',...etc]

time_stamp是'numpy.string _'

类型的列表

我收到以下错误

Traceback (most recent call last):
  File "/home/joseph/PycharmProjects/demo/src/alarm_log/alarm_db_plot.py", line 23, in <module>
    plt.plot(time_stamp, metric)
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3154, in plot
    ret = ax.plot(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1814, in inner
    return func(ax, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 1425, in plot
    self.add_line(line)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1708, in add_line
    self._update_line_limits(line)
  File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1730, in _update_line_limits
    path = line.get_path()
  File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 925, in get_path
    self.recache()
  File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 612, in recache
    x = np.asarray(xconv, np.float_)
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 482, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: invalid literal for float(): 2017:02:01:14:44:00

我想我可能需要使用matplotlib dateformatter来解析time_stamp。我试过了,但我无法弄清楚如何。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

在不看数据的情况下不确定您要绘制的内容,但您可以尝试转换string to a datetime object。有关格式代码的列表,请参阅here

SELECT count(ID)+1 from money_receipt;