1

I've got a bunch of data that I read in from a telnet machine...It's x, y data, but it's just comma separated like x1,y1,x2,y2,x3,y3,...

Here's a sample:

In[28] data[1:500]
Out[28]  b'3.00000000E+007,-5.26880000E+001,+3.09700000E+007,-5.75940000E+001,+3.19400000E+007,-5.56250000E+001,+3.29100000E+007,-5.69380000E+001,+3.38800000E+007,-5.40630000E+001,+3.48500000E+007,-5.36560000E+001,+3.58200000E+007,-5.67190000E+001,+3.67900000E+007,-5.51720000E+001,+3.77600000E+007,-5.99840000E+001,+3.87300000E+007,-5.58910000E+001,+3.97000000E+007,-5.35160000E+001,+4.06700000E+007,-5.48130000E+001,+4.16400000E+007,-5.52810000E+001,+4.26100000E+007,-5.64690000E+001,+4.35800000E+007,-5.3938'

I want to plot this as a line graph with matplot lib. I've tried the struct package for converting this into a list of doubles instead of bytes, but I ran into so many problems with that...Then there's the issue of what to do with the scientific notation...I want to obviously perserve the magnitude that it's trying to convey, but I can't just do that by naively swapping the byte encodings to what they would mean with a double encoding.

I'm trying all sorts of things that I would normally try with C, but I can't help but think there's a better way with Python!

I think I need to get the x's and y's into a numpy array and do so without losing any of the exponential notation...

Any ideas?

testname123
  • 1,061
  • 3
  • 20
  • 43

2 Answers2

4

First convert your data to numbers with for example:

data = b'3.00000000E+007,-5.26880000E+001,+3.09700000E+007,-5.75940000E+001'
numbers = map(float, data.split(','))

Now slice the array to get x and y-data seperately and plot it with matplotlib

x = numbers[::2]
y = numbers[1::2]

import matplotlib.pyplot as plt
plt.plot(x, y)
plt.show()
pathoren
  • 1,634
  • 2
  • 14
  • 22
  • This produces an error: `TypeError: a bytes-like object is required, not 'str'`....This is strange because `type(data)` returns `bytes`, not `str`. – testname123 Aug 04 '16 at 13:43
  • It works flawlessly for me in iPython, so Im afraid I cannot reproduce your problem. What python version are you using? – pathoren Aug 04 '16 at 18:04
  • 3.0...I resorted to converting it to a string and then stuffing that into a numpy array...The rest of the code works as described, thank you. – testname123 Aug 04 '16 at 18:30
1

to read your items, two at a time (x1, y1), try this: iterating over two values of a list at a time in python

and then (divide and conquer style), deal with the scientific notation separately.

Once you've got two lists of numbers, then the matplotlib documentation can take it from there.

That may not be a complete answer, but it should get you a start.

Community
  • 1
  • 1
mdev
  • 71
  • 1
  • 6