0

I have simple python code to read data file and plot graph. It works well in previous system. My system was crashed, I setting another system with higher ubuntu version. When I run script, I faced error which is not exist in previous system. Previous system was broken, I cannot compare in detail.

Code

 import matplotlib.pyplot as plt
 import csv
 import sys
 def analyze():

     datafile = sys.argv[1]
     pieces = []
     with open(datafile, 'rt') as f:
          data = csv.reader(f,delimiter = '\t')
          for d in data:
            pieces.append(d)

    x = [op for op, response, interval in pieces]
    #y1 = [response for op, response, interval in pieces]
    y1 = [interval for op, response, interval in pieces]

    p1 = plt.plot (x, y1, label='test')
    plt.legend()
    plt.title ('AAAAA test')
    plt.xlabel('# of Operation'), plt.ylabel('Response time')

    #plt.hist(y1, 100)
    plt.show()

if __name__ == '__main__':
    analyze()

DATA FIle

    0             277.94                0.00
     1            277.94             6553.00
     2            277.94            32768.00
     3            277.94            32768.00
     4            277.94            45875.00
     5            277.94            13108.00
     6            277.94            26215.00
     7            232.93             6553.00
     8            277.94            13107.00
     9            232.93            39321.00
    10            301.94             6554.00
    11            232.93             6554.00
    12            277.94            13108.00
    13            232.93            19661.00
    14            236.93             6554.00
    15            285.94            19661.00
    16            232.93            32768.00
    17            277.94             6554.00
    continue......

Error (Python 2.7)

 Traceback (most recent call last):
   File "../scripts/plot_interval.py", line 27, in <module>
     analyze()
   File "../scripts/plot_interval.py", line 18, in analyze
     p1 = plt.plot (x, y1, label='test')
   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/lib/python2.7/dist-packages/numpy/core/numeric.py", line 482, in asarray
     return array(a, dtype, copy=False, order=order)
 ValueError: could not convert string to float: ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

Error (python3)

 Traceback (most recent call last):
   File "../scripts/plot_interval.py", line 27, in <module>
     analyze()
   File "../scripts/plot_interval.py", line 11, in analyze
     for d in data:
   File "/usr/lib/python3.5/codecs.py", line 321, in decode
     (result, consumed) = self._buffer_decode(data, self.errors, final)
 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe6 in position 0:  invalid continuation byte
3
  • try print the two arrays and post the results Commented Jan 20, 2017 at 11:48
  • I can run the script with the given example data. Could there be some sort of String / char in the data file? Commented Jan 20, 2017 at 12:00
  • I solved the issue, thanks Commented Jan 20, 2017 at 13:42

1 Answer 1

1

I can suggest a very easy debug step:

Before returning the array, print the var a and see if there are strings that do not represent a valid float.

When you find them, you will need to know why they are there.

Sign up to request clarification or add additional context in comments.

1 Comment

I found the reason that there is crash in the middle of data file.

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.