简体   繁体   English

Python实时绘图

[英]Python real time plotting

I have a problem... I've already tried some ways but it didn;t work. 我有问题...我已经尝试了一些方法,但是没有用。 I have to do a real time data aquisition and plotting them in an interface... If you can suggest me a way to do that... The program below makes one data aquisition in variable "data"(matrix), but I have to do it continuously and plotting them the same time... Thank you! 我必须进行实时数据采集并将其绘制在界面中...如果您可以建议我一种方法...下面的程序在变量“ data”(矩阵)中进行一个数据采集,但是我有连续进行并同时绘制它们...谢谢!

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

What about something like this (I assumed you were plotting your data against time): 这样的事情怎么样(我假设您是根据时间绘制数据的):

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)

and to start the reading/plotting: 并开始阅读/绘图:

t = test()
t.start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM