how to plot a list of byte data with matplotlib - python

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?

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()

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.

Related

How to remove scientific notation on bqplot?

I am using bqplot to create a live line graph on jupyter-notebook + VOILA
from bqplot import pyplot as plt2
import datetime
x_values = [] #array of datetimes
y_values = [] #array of 10+ digit numbers
plt2.show()
def functionThatIsCalledRepeatedly(x_val, y_val):
x_values.append(x_val)
y_values.append(y_val)
plt2.plot(x_values, y_values)
Part of the Resulting Plot
My question is, how do I remove the scientific notation from the y-axis. It's a simple task but I have tried a lot of things.
I tried using axes.tick_format property of the graph but I think that only works if you have axes objects which I cannot have because they require the mandatory Scale property which I cannot use because the graph is live and the x and y scales need to be generated/recalibrated while it runs.
I tried changing y_values.append(y_val) to y_values.append("{:.2f}".format(y_val)) but that converts to a string and bqplot doesn't process it as a number so it ends up with negative numbers on top of the 0 sometimes.
I tried converting to a numpy array and then doing np.set_printoptions(suppress=True) which (obviously) didn't work.
Basically tried a lot of things and I think it comes down to some bqplot property that may or may not exist. Have been stuck for a while. Thank you!
You can provide axes options with the tick format you want to the plot method:
plt2.plot(x_values, y_values, axes_options={
y=dict(tick_format='0.2f')
})
You can see examples of this axes_options (using a scatter plot, but that should work the same) in this notebook: https://github.com/bqplot/bqplot/blob/master/examples/Marks/Pyplot/Scatter.ipynb

Using Python To Plot Live Lidar Data Causing Circular Plots

I am using the RPLidar A1: https://www.adafruit.com/product/4010
My goal is to collect data sets and plot them in order to get a live visual representation of the data.
My current code is:
import numpy as np
import matplotlib.pyplot as plt
from rplidar import RPLidar
def get_data():
lidar = RPLidar('COM6', baudrate=115200)
for scan in lidar.iter_scans(max_buf_meas=500):
break
lidar.stop()
return scan
for i in range(1000000):
if(i%7==0):
x = []
y = []
print(i)
current_data=get_data()
for point in current_data:
if point[0]==15:
x.append(point[2]*np.sin(point[1]))
y.append(point[2]*np.cos(point[1]))
plt.clf()
plt.scatter(x, y)
plt.pause(.1)
plt.show()
The above code produces a refreshing graph with changing data as shown below:
The issue is that this is not an accurate representation. There is a native application by SLAMTEC called frame_grabber which clearly shows this device giving accurate rectangular shaped representation of my room. Instead I keep getting a circular shape ranging from small to large.
The raw data from the sensor comes in the form of an array containing roughly 100 sets of the following data: (quality, theta, r). My code checks if the quality is good (15 is maximum), and then goes on to plot data sets and clears the data array every seven instances in order to get rid of old data.
I checked the raw polar data in excel and the data also appears to create a circularish shape.
After a few days of trying out various libraries for plotting and trying a few other things I've noticed the mistake.
Hopefully this can prevent someone from making the same mistake in the future.
The lidars typically give data in terms of "theta" and "r". On the other hand the numpy as well as the built in math library in Python accept arguments in radians for performing cos and sin operations.
I've converted the units from degrees to radians and now everything works perfectly.

Pyplot Scatter Plot

I'm not great at matplotlib, but I need to use it for some work I am doing. I have a set of 9 columns of data, with around 100k lines. I want to produce a scatter plot, and I don't care about the rows, they're meaningless for my purposes. What I need is for the values to be plotted as a scatter against which column they are in, regardless of which row they a part of.
This is all loaded in from a text file in a simple 2D array using numpy.loadtxt. It's just a set of numbers, so any substitution of random numbers should work. I'm just not sure how to manipulate it in a way that the scatter command will like. I often get it giving me errors like I'm giving it too few arguments, or it just iterates over the array (or arrays if I separate them), in ways I do not anticipate.
My first thought is that I could somehow break it down into a set of series by column, but I don't think the scatter command will take that. Any help would be very much appreciated.
The scatter function takes in two lists that have the same length. To access a single column n of your numpy array, just use data[:, n]. Since you want to correspond all columns with their column number, you need to also create a list that has the same length of data, with only the column number as elements. To create the plot you want, just do the following:
for i in range(9):
plt.scatter([i + 1] * len(data), data[:, i])

python plot 2d numpy array

I am having a bit of misunderstanding with numpy array
I have a set of two data (m, error) which I would like to plot
I save them in the array like this as I catch them in the same loop (which is probably causing the issue)
sum_error = np.append(m,error)
Then just simply trying to plot like this but it doesn't work as apparently this array is only of size 1 (with a tuple?)
plt.scatter(x=sum_error[:, 0], y=sum_error[:, 1])
What is the correct way to proceed? should I really make two different arrays, one for m, one for error in order to plot them?
Thanks
As it is answered on this thread, try using
np.vstack((m,error))

Python 2.7 time series non numeric values

I am using Python 2.7 and need to draw a time series using matplotlib library. My y axis data is numeric and everything is ok with it.
The problem is my x axis data which is not numeric, and matplotlib does not cooperate in this case. It does not draw me a time series even though it is not supposed to affect the correctness of the plot, because the x axis data is arranged by a given order anyway and it's order does not affect anything logically.
For example let's say the x data is ["i","like","python"] and the y axis data is [1,2,3].
I did not add my code because I've found that the code is ok, it works if I change the data to all numeric data.
Please explain me how can I use matplotlib to draw the time series, without making me to convert the x values to numeric stuff.
I've based my matplotlib code on following answers: How to plot Time Series using matplotlib Python, Time Series Plot Python.
Matplotlib requires someway of positioning those labels. See the following example:
import matplotlib.pyplot as plt
x = ["i","like","python"]
y = [1,2,3]
plt.plot(y,y) # y,y because both are numeric (you could create an xt = [1,2,3]
plt.xticks(y,x) # same here, the second argument are the labels.
plt.show()
, that results in this:
Notice how I've put the labels there but had to somehow say where they are supposed to be.
I also think you should put a part of your code so that it's easier for other people to suggest upon.

Categories

Resources