python - matplotlib - plotting a timeseries using matplotlib -
03/12 20:23:26.11: 04:23:26 l9 <mx acc magnum xdv:00111a0000000117 00d3001200870172 01ff6000f01cfe81 3d26000000000300 03/12 20:23:26.11: 04:23:26 l9 <mx acc mid 0x1500 len 26 xdv:00111a0000000117 00d3001200870172 01ff6000f01cfe81 3d26000000000300 03/12 20:23:26.11: 04:23:26 l8 <mx jk31 (mx) jsp:17.37.6.99: size = 166, data: 00345c4101003031 e463ef0113108701 5a01ff6008f01cfe 81ab170000000003 ef01131087015a01 ff6008f01cfe81ab 170000000003ef01 131087015b01ff60 00f01cfe81701b00 00000003ef011310 87015b01ff6000f0 1cfe81701b000000 0003ef0113108701 5c01ff2000f01cfe 81cb240000000003 ef01131087015c01 57cc00f01cfe81cb 240000000003ef01 131087015d01ff20 00f01cfe815b2900 00000003ef011310 87015d01ff2000f0 1cfe815b29000000 0003ef0113108701 5e01ff6000f01cfe 819d280000000003 ef01131087015e01 ff6000f01cfe819d 0003 03/15 20:23:26.11: 04:23:26 l8 <kx jk49 (kx) jsp:15.33.2.93: size = 163, data: 00647741000030ef 01131087015a01ff 6008f01cfe81ab17 0000000003ef0113 1087015a01ff6008 f01cfe81ab170000 000003ef01131087 015b01ff6000f01c fe81701b00000000 03ef01131087015b 01ff6000f01cfe81 701b0000000003ef 01131087015c01ff 2000f01cfe81cb24 0000000003ef0113 1087015c01ff2000 f01cfe81cb240000 000003ef01131087 015d01ff2000f01c fe815b2900000000 03ef01131087015d 01ff2000f01cfe81 5b290000000003ef 01131087015e01ff 6000f01cfe819d28 0000000003ef0113 1087015e01ff6000 f01cfe819d280000 a6220000000003 03/15 20:23:26.11: 04:23:26 l8 <kx jk21 (kx) jsp:10.22.1.53:size = 163, data: 009d1141000030ef 01131087015a01ff 6008f01cfe81ab17 0000000003ef0113 1087015a01ff6008 f01cfe81ab170000 000003ef01131087 015b01ff6000f01c fe81701b00000000 03ef01131087015b 01ff6000f01cfe81 701b0000000003ef 01131087015c01ff 2000f01cfe81cb24 0000000003ef0113 1087015c01ff2000 f01cfe81cb240000 000003ef01131087 015d01ff2000f01c fe815b2900000000 03ef01131087015d 01ff2000f01cfe81 5b290000000003ef 01131087015e01ff 6000f01cfe819d28 0000000003ef0113 1087015e01ff6000 f01cfe819d280000 a6220000000003
i have following data extracted data above. contains time , number.i want plot data timeseries using matplotlib.
04:20:54 491 04:21:02 33 04:21:04 1063 04:21:04 1063 04:21:04 711 04:21:09 56 04:21:12 73 04:21:14 1066 04:21:14 931 04:21:18 618 04:21:18 51 04:21:22 27 04:21:24 1063 04:21:24 1063 04:21:24 535 04:21:33 24 04:21:33 1063 04:21:33 1063 04:21:33 978 04:21:43 36 04:21:45 1063 04:21:45 1063 04:21:45 755 04:21:53 27 04:21:55 1066 04:21:55 1063 04:21:55 711 04:22:03 30 04:22:05 1069 04:22:05 1063 04:22:05 1063 04:22:05 450 04:22:10 56 04:22:12 76 04:22:15 1066 04:22:15 1063 04:22:15 1066
i following code.
import matplotlib.pyplot plt match = ("l8 <mx jk31 (mx)") open("test.txt") fin: print(' : {}', fin.name) line in fin: if match in line: line = line.strip.split() time = line[2] size = line[9].strip(",") plt.plot(time, data_size)
i getting following error.
04:00:07 27 traceback (most recent call last): file "sawe_issue.py", line 16, in <module> plt.plot(time, data_size) file "c:\anaconda3\lib\site-packages\matplotlib\pyplot.py", line 3099, in plot ret = ax.plot(*args, **kwargs) file "c:\anaconda3\lib\site-packages\matplotlib\axes\_axes.py", line 1373, in plot line in self._get_lines(*args, **kwargs): file "c:\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 304, in _grab_next_args seg in self._plot_args(remaining, kwargs): file "c:\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 263, in _plot_args linestyle, marker, color = _process_plot_format(tup[-1]) file "c:\anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 115, in _process_plot_format 'unrecognized character %c in format string' % c) valueerror: unrecognized character 7 in format string
you can use parser
package dateutil
. handles lot of common formats without having specify format strings.
from dateutil import parser import matplotlib.pyplot plt match = ("l8 <mx jk31 (mx)") open("test.txt") fin: print(' : {}', fin.name) time_data = [] size_data = [] line in fin: if match in line: line = line.strip.split() time_str = line[2] t = parser.parse(time_str) ## note: changed 'time' 't', because it's bad idea use 'time' variable name, since python built-in time_data.append(t) size = int(line[9].strip(",")) size_data.append(size) plt.plot(time_data, size_data)
also note: parser.parse() returns datetime object includes year/month/day value. if none specified (as in example), year/month/day set current day.
update: here's way generalize multiple match strings:
from dateutil import parser import matplotlib.pyplot plt match_list = ["l8 <mx jk31 (mx)", "l9 <mx jk31 (mx)"] ## put match strings in list open("test.txt") fin: print(' : {}', fin.name) time_data = {} ## save data in dictionaries, string keys , lists values size_data = {} line in fin: match in match_list: if match in line: if match not in time_data: time_data[match] = [] ## initialize empty list first time key encountered size_data[match] = [] line = line.strip.split() time_str = line[2] t = parser.parse(time_str) time_data[match].append(t) size = int(line[9].strip(",")) size_data[match].append(size) match in match_list: plt.figure() ## create new figure each data set plt.plot(time_data[match], size_data[match]) plot.show() ## simultaneously show plots
Comments
Post a Comment