使用for-loop值创建基本图形 [英] Using for-loop values to create a basic graph

查看:141
本文介绍了使用for-loop值创建基本图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用ctypes在python中包装了一个C库并且调用了这个DLL。然后我创建了一个for-loop,打印出我所有的数据。

 从ctypes导入ctypes 
导入*
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)





$ b我想创建一个简单的图形,其数据点是从for循环打印出来。这是我迄今为止:

 为我在范围内(0,1032):
x =(g [ 0] .time [i])
y =(g [0] .data [0] .f [i])
print(i,x,y)

这个for循环可以完美的工作,把所有的数据打印出来,但是我需要把这个列表绘制成一个简单的图。 for循环的返回如下所示:

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



<第二列对应于时间值,第三列是当时的数据值。

我将x和y设置为指向包含所有数据的对象的指针。
我很难将这些数据转化为图表。 idk我做错了什么,因为在大多数matplotlib的例子,他们只是创建随机数据并绘制它。我需要一种方法将数据存入图表。
这是行不通的:
$ b $ pre $ $ $ $ $
$ )

它构建一个图形,但不会将数据填充到图形中。这是我的图表看起来不太好。


我可以从打印的数值列表中获取我的数据到一个基本的图表吗?解决方案

x和y是单浮点数,而不是数组,如print语句的输出所示。 plt.plot()尝试在点之间绘制线条,但由于只有一个点,所以不会有线条。我会假设你想以某种方式使x和y数组的值也许是这样的... ...
$ b $ pre $ x = []
y = []
for i in range(0,1032):
x.append(g [0] .time [i])
y.append(g [0 ] .data [0] .f [i])

然后当传递给 plt.plot 如果要在非连接点上绘制一个散点图,则有多个点交替绘制
,使用 plt.scatter


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.

My question is this:

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

解决方案

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

这篇关于使用for-loop值创建基本图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆