colinoflynn/pico-python

Visualization: Trying to regenerate plot (cannot clear previous plots)

AntonyLHart opened this issue · 2 comments

Hi guys. So I have taken the ps2000 example and attempted to add the runblock into a while loop as to regenerate the output plot... I am using an external sig gen to produce the 50Khz, and then change slightly.

ISSUE 1: I cannot seem to find a way to clear the previous plot, although the updated plot info is sourced from 'dataA', each plot seems to be a new plot... this must also be causing me memory issues, surely? Any ideas on how to update the original plot instead of creating new ones each time the while loop executes?

ISSUE 2: Any ideas on the try: except KeyboardInterrupt: break from while loop, this doesnt seem to work either?

All suggestions appreciated.

"""
Autotest

By: By Antony Hart, based on Colin O'Flynn and Mark Harfouche's software

PicoScope 2205A
"""
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import time
from picoscope import ps2000
import pylab as plt
import numpy as np

if __name__ == "__main__":
    print(__doc__)

    print("Attempting to open Picoscope 2000...")

    ps = ps2000.PS2000()

    print("Found the following picoscope:")
    print(ps.getAllUnitInfo())

    waveform_desired_duration = 50E-6
    obs_duration = 3 * waveform_desired_duration
    sampling_interval = obs_duration / 4096

    (actualSamplingInterval, nSamples, maxSamples) = \
        ps.setSamplingInterval(sampling_interval, obs_duration)        
    
    channelRange = ps.setChannel('A', 'DC', 2.0, 0.0, enabled=True,
                                 BWLimited=False)

    ps.setSimpleTrigger('A', 1.0, 'Falling', timeout_ms=100, enabled=True)

    fig = plt.figure()      
    plt.grid(True, which='major')
    plt.title("Picoscope 2000 waveforms")
    plt.ylabel("Voltage (V)")
    plt.xlabel("Time (ms)")          
    plt.show(block=False)

    while True:
        try:
            ps.runBlock()
            ps.waitReady()
            #print("Waiting for awg to settle.")
            ps.runBlock()
            ps.waitReady()
            #print("Done waiting for trigger")
            dataA = ps.getDataV('A', nSamples, returnOverflow=False)
            dataTimeAxis = np.arange(nSamples) * actualSamplingInterval

            plt.plot(dataTimeAxis, dataA, label="Clock")
            fig.canvas.draw()                          
        
        except KeyboardInterrupt:
            break 

    ps.stop()
    ps.close() 

I get that you are facing this issue in learning how to use the Picoscope, but this is really a question for the matplotlib guys on stack overflow.

  1. See if this might help. https://stackoverflow.com/questions/4098131/how-to-update-a-plot-in-matplotlib
  2. I really don't know sorry :/ Once again, stack overflow is your friend. The answer is likely there.

Don't post a code depending on picopython, they won't know what is. Just generate data using numpy random or something.

If there isn't anything we can help you with on the Python <-> Picoscope side, please feel free to close this issue.

okay, thanks