-1

I have wrapped a C library in python using ctypes and called the DLLs. I then created a for-loop that prints out all of my data. Here is a small sample of the code that I have wrapped.

import ctypes 
from ctypes import *
import matplotlib.pyplot as plt

class ParmData(Union):
_fields_ = [
     ('c', ctypes.POINTER(ctypes.c_ubyte)),
     ('f', ctypes.POINTER(ctypes.c_float))]
class SParm(Structure):
pass
 SParm._fields_ = [
    ('data', ctypes.POINTER(ParmData)),
    ('time', ctypes.POINTER(ctypes.c_float))]

dll.readSParm.argtypes = (POINTER(SFile), c_char_p, c_double, c_double,    c_double, POINTER(TTag), c_ushort,)   
dll.readSParm.restype = POINTER(SParm)
g = dll.readSParm(SF, ParmName, startTime, stopTime, Null, None, convertType)
dll.freeSParm(g)

I want to create a simple graph with data points that are printed out from a for loop. This is what I have so far:

for i in range(0, 1032):
x = (g[0].time[i])
y = (g[0].data[0].f[i])
print(i, x, y)

This for-loop works perfectly and prints out all of the data into a neat list, but I need that list to be plotted into a simple graph. The return of the for-loop looks like this:

1028 -1.2241029739379883 1.7323481895261962e+19
1029 -0.22411400079727173 2.635080461412465e+23
1030 0.7759910225868225 1.7829588197038883e+19
1031 1.7759920358657837 1.8027558490491145e+28

Where the second column corresponds to the time value and the third column is the data values for that time.

I set x and y equal to pointers to objects that are contain all of the data. I'm having a difficult time getting that data into a graph. idk what i'm doing wrong because in most of the matplotlib examples they just create random data and plot it. I need a way to get my data into the graph. This doesn't work:

plt.plot(x, y)
plt.show()

It builds a graph, but it doesn't populate the data into the graph. This is what my graph looks like which is no good. Bad Graph

My question is this:

how can I get my data from a printed list of value pairs into a basic graph?

3
  • Have you tried giving the line a color? plt.plot(x,y,'-r') Commented Jul 14, 2017 at 19:10
  • Your x and y get reinitialised in the for loop, I guess you might want to build a list instead. Commented Jul 14, 2017 at 19:15
  • 1
    I guess that using plot(g[0].time, g[0].data[0].f) should work? Commented Jul 14, 2017 at 19:15

1 Answer 1

0

x and y are single floats, not arrays as indicated by the output of your print statement. plt.plot() tries to draw lines between points, but since there is only one point, there can be no line. I would assume you want to somehow make x and y into arrays of values perhaps something like this...

x = []
y = []
for i in range(0, 1032):
    x.append(g[0].time[i])
    y.append(g[0].data[0].f[i])

then when passed to plt.plot there is more than one point to plot alternately if you want a scatter plot on non-connected points, use plt.scatter

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

2 Comments

Why populate a new list when the data is already in g[0].time and g[0].data[0].f?
@Bart I thought about that, but was unsure if this object's fields supported the full __slice__ notation given that it was from a custom wrapped library. I'm not too sure how that whole process shakes down.

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.