1
  03/12 20:23:26.11: 04:23:26 L9 <Mx  Acc  Magnum All            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 the following data extracted from the data above. which contains time and a number.I want to plot the data as a 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 was the following code.

import matplotlib.pyplot as plt

match = ("L8 <Mx JK31 (Mx)")
with open("test.txt") as fin:
print(' : {}', fin.name)
for line in fin:
    if match in line:
        line = line.strip.split()
        time = line[2]
        size = line[9].strip(",")
        plt.plot(time, data_size)

I am getting the 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
    for line in self._get_lines(*args, **kwargs):
  File "C:\Anaconda3\lib\site-packages\matplotlib\axes\_base.py", line 304, in _grab_next_args
    for 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
5
  • 1
    Can you post the code used to extract the data? It would help to know the type of the time variable. Commented Apr 19, 2016 at 20:25
  • @FCo - please see update to question. Commented Apr 19, 2016 at 20:35
  • @FCo - it is of type str Commented Apr 19, 2016 at 20:36
  • 1
    You are not converting your time to a number, you are trying to plot a string. Commented Apr 19, 2016 at 20:37
  • @gariepy - how do I convert the time string to a simple number? Commented Apr 19, 2016 at 20:38

1 Answer 1

1

You can use the parser package from dateutil. It handles a lot of common formats without having to specify format strings.

from dateutil import parser

import matplotlib.pyplot as plt

match = ("L8 <Mx JK31 (Mx)")
with open("test.txt") as fin:
    print(' : {}', fin.name)
    time_data = []
    size_data = []
    for line in fin:

        if match in line:
           line = line.strip.split()
           time_str = line[2]
           t = parser.parse(time_str)  ## NOTE: changed 'time' to 't', because it's a bad idea to use 'time' as a variable name, since it is a 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 a datetime object that includes a year/month/day value. If none is specified (as in your example), the year/month/day will be set to the current day.

UPDATE: Here's a way to generalize for multiple match strings:

from dateutil import parser

import matplotlib.pyplot as plt

match_list = ["L8 <Mx JK31 (Mx)", "L9 <Mx JK31 (Mx)"]  ## put all match strings in this list
with open("test.txt") as fin:
    print(' : {}', fin.name)
    time_data = {}  ## save data in dictionaries, with string keys and lists as values
    size_data = {}
    for line in fin:
        for match in match_list:
            if match in line:
               if match not in time_data:
                   time_data[match] = []  ## initialize empty list the first time this key is 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)
    for match in match_list:
        plt.figure()  ## create a new figure for each data set
        plt.plot(time_data[match], size_data[match])
    plot.show()  ## simultaneously show all plots
Sign up to request clarification or add additional context in comments.

8 Comments

That's probably coming from your data_size argument...are you converting that from a string to an int (or a float)?
When you call plt.plot(), and one of the arguments is a string,` plot()` expects that string to be a format string for plotting, which has a very restricted set of characters, and format -- hence the error you keep seeing. You just need to make sure you are converting your data into numeric formats.
I fixed that.i.e converted the size from str to int by casting. I am getting an empty graph with X and Y axis which has the time range and size range but not plots. :(
Rather than issuing one plot command per data point, what you should do is aggregate the data into arrays, and issue a single plot command. I will update the answer.
thanks.I have one more query. In the match If I want to search for more strings and plot those in separate graphs how do I do that.?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.