1

I'm trying to plot time series data using matplotlib using the following code

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))

The format of time_stamp is

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

time_stamp is a list of type 'numpy.string_'

I'm getting the following error

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

I think I might need to used matplotlib dateformatter to parse the time_stamp. I tried so but I couldn't figure out how. Any help please?

Joseph Wahba
  • 660
  • 3
  • 9
  • 25

1 Answers1

0

Not exactly sure what you're trying to plot without seeing the data, but you could try to convert the string to a datetime object. See here for a list with the format codes.

from datetime import datetime
time_stamp = ['2017:02:01:14:44:00', '2017:02:01:14:44:01']
time_stamp = [datetime.strptime(i, '%Y:%m:%d:%H:%M:%S') for i in time_stamp]
print(time_stamp)
Chris
  • 1,287
  • 12
  • 31