首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python实时绘图

Python实时绘图
EN

Stack Overflow用户
提问于 2017-04-03 13:30:18
回答 1查看 464关注 0票数 0

我有个问题..。我已经尝试过一些方法,但没有成功。我必须做一个实时的数据采集并在一个接口中绘制它们.如果你能建议我这样做的话.下面的程序在变量“数据”(矩阵)中进行一次数据采集,但我必须不断地进行,并同时绘制它们.谢谢!

代码语言:javascript
复制
# Print library info:
print_library_info()

# Search for devices:
libtiepie.device_list.update()

# Try to open an oscilloscope with block measurement support:
scp = None
for item in libtiepie.device_list:
    if item.can_open(libtiepie.DEVICETYPE_OSCILLOSCOPE):
        scp = item.open_oscilloscope()
        if scp.measure_modes & libtiepie.MM_BLOCK:
            break
        else:
            scp = None

if scp:
    try:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        k=0
        while k<20:
            # Set measure mode:
            scp.measure_mode = libtiepie.MM_BLOCK

            # Set sample frequency:
            scp.sample_frequency = 5e6  # 1 MHz

            # Set record length:
            scp.record_length = 1000  # 15000 samples

            # Set pre sample ratio:
            scp.pre_sample_ratio = 0  # 0 %

            # For all channels:
            for ch in scp.channels:
                # Enable channel to measure it:
                ch.enabled = True

                # Set range:
                ch.range = 8  # 8 V

                # Set coupling:
                ch.coupling = libtiepie.CK_ACV  # DC Volt

            # Set trigger timeout:
            scp.trigger_time_out = 100e-3  # 100 ms

            # Disable all channel trigger sources:
            for ch in scp.channels:
                ch.trigger.enabled = False

            # Setup channel trigger:
            ch = scp.channels[0]  # Ch 1

            # Enable trigger source:
            ch.trigger.enabled = True

            # Kind:
            ch.trigger.kind = libtiepie.TK_RISINGEDGE  # Rising edge

            # Level:
            ch.trigger.levels[0] = 0.5  # 50 %

            # Hysteresis:
            ch.trigger.hystereses[0] = 0.05  # 5 %

            # Print oscilloscope info:
            #print_device_info(scp)

            # Start measurement:
            scp.start()

            # Wait for measurement to complete:
            while not scp.is_data_ready:
                time.sleep(0.01)  # 10 ms delay, to save CPU time

            # Get data:
            data = scp.get_data()




    except Exception as e:
        print('Exception: ' + e.message)
        sys.exit(1)

    # Close oscilloscope:
    del scp

else:
    print('No oscilloscope available with block measurement support!')
    sys.exit(1)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-20 23:14:51

像这样的事情怎么样(我以为你是在根据时间绘制你的数据):

代码语言:javascript
复制
import joystick as jk
import time

class test(jk.Joystick):
    _infinite_loop = jk.deco_infinite_loop()
    _callit = jk.deco_callit()

    @_callit('before', 'init')
    def _init_data(self, *args, **kwargs):
        self.t = np.array([])
        self.data = np.array([])
        # do the hardware initialization here
        #.............
        self.range = 8
        self.record_length = 1000

    @_callit('after', 'init')
    def _build_frames(self, *args, **kwargs):
        self.mygraph = self.add_frame(
                    Graph(name="Graph", size=(500, 500),
                          pos=(50, 50), fmt="go-", xnpts=self.record_length,
                          freq_up=10, bgcol="w", xylim=(0,10,0,self.range)))

    @_callit('before', 'start')
    def _set_t0(self):
        # initialize t0 at start-up
        self._t0 = time.time()

    @_infinite_loop(wait_time=0.2)
    def _get_data(self):
        self.t = self.mygraph.add_datapoint(self.t, time.time())
        # new data acquisition here
        new_data = scp.get_data()
        self.data = self.graph.add_datapoint(self.data, new_data)
        self.mygraph.set_xydata(self.t-self._t0, self.data)

并开始阅读/策划:

代码语言:javascript
复制
t = test()
t.start()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43185989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档