4

I'm trying to use matplotlib to plot binary data read from a file:

import matplotlib.pyplot as plt

try:
    f = open(file, 'rb')
    data = f.read(100)
    plt.plot(data)
except Exception as e:
    print(e)
finally:
    f.close()

But I got the following error:

'ascii' codec can't decode byte 0xfd in position 0: ordinal not in range(128)

The file I'm reading consists of binary data. So how does matplotlib treat binary data? Is it unsigned or signed 1 byte data?

11
  • How do you expect matplotlib to interpret random binary data? What sort of plot are you looking for? Commented Mar 18, 2013 at 20:45
  • 1
    @askewchan If I print <code>data</code>, it prints as <code>b'\xfd\xd0\xfb\xcaM......</code>. So I need a parser to parse binary data to ASCII data format for plot function, right? Commented Mar 18, 2013 at 21:06
  • 2
    @tonga Binary data can be stored in a million different ways (actually a lot more :)). For example, you might have 4-byte long integer values in that binary file, or you might have 64-bit floating point values, or you might have a bzipped-text file, etc. etc. You need to first find out how data is organized in the binary data file. Post this information as part of this question. The answer will probably be obvious at that stage. Commented Mar 18, 2013 at 21:51
  • 1
    Also, if you know the type/format of data that is stored in the binary file, you can use numpy.fromfile to read it into an array. Commented Mar 18, 2013 at 22:00
  • 2
    @tonga - As crazeewulf already pointed out, numpy.fromfile is quite handy for what you're describing (There's also numpy.fromstring and frombuffer, if the data isn't in a file). If you want unsigned chars, then do data = numpy.fromfile(yourfile, dtype=numpy.uint8), if you want signed chars, then do data = numpy.fromfile(yourfile, dtype=numpy.int8). If you don't want to use numpy have a look at the builtin struct or array modules. Either way, if you want uints, you need to convert the string to a sequence of uints. Hopefully that helps clarify things a touch. Commented Mar 18, 2013 at 23:37

1 Answer 1

5

As has been pointed out in the comments of your question, the bytes you are passing to plot are ambiguous. You need to turn those bytes into a numpy array (or a list/tuple) before passing it to matplotlib.

A simple example to demonstrate this:

import numpy as np
import matplotlib.pyplot as plt


orig_array = np.arange(10, dtype=np.uint8)
with open('my_binary_data.dat', 'wb') as write_fh:
    write_fh.write(orig_array)

with open('my_binary_data.dat', 'rb') as fh:
    loaded_array = np.frombuffer(fh.read(), dtype=np.uint8)

print loaded_array
plt.plot(loaded_array)
plt.show()

I've gone around the houses in demonstrating using numpy.frombuffer with the bytes you read into your "data" variable, but in truth you would just use numpy.fromfile so that the loading line looks like:

loaded_array = np.fromfile(fh, dtype=np.uint8)

HTH

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

Comments

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.