Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

python - Using for-loop values to create a basic graph

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...