<p>Being new to Python and this community, I am trying to plot the pulse return from a transducer. The pulse return is 400 uSecs in duration and occurs every 100 milliseconds. So far I can plot the received values that are constantly being received and the plot graphs these in a strip chart fashion scrolling from right to left. I would like to be able to present the plot similar to how an oscilloscope would display the return pulse. I am not all that familiar with Numpy arrays yet. I am not sure what I am forgetting in writing the proper syntax for this. Here is a snippet of the code I am having issues with.</p>
<pre><code>### Parameters   ##################
x = 300            # Number of points to display

y_range = [0,300]   # Range of possible Y values to display
#y_range = list(np.arange(0, 300))
xs = list(np.arange(0, 300))
ys = [0] * x
plot1.set_ylim(y_range)

###  Initialize communication with the pico and read value from ADC
reading = analogio.AnalogIn(board.ADC0)

###   Create plot line
line, = plot1.plot(xs, ys, color= &quot;yellow&quot;)

###   Start Thread
def plot():
    def animate(i, ys):
    # Read data from Pico
        DATA = reading.value/20
    
    #   Add y to list
        ys.append(DATA)
    
    #   Limit y list to set number of items
        ys = ys[- x:]
        #ys = ys(np.arange(0, 300))
    
    #   Update line with new y values
        line.set_ydata(ys)
        print(&quot;ADC:&quot;, DATA) 
        return line,

### Set up plot to call animate() function
    ani = animation.FuncAnimation(fig, animate, fargs=(ys,),interval=.0001, blit=True)
    
    

print(&quot;thread starting&quot;)
t = threading.Thread(name='plot', target=plot)
t.setDaemon(True)
t.start()
</code></pre>

CC BY-SA 4.0
2021-09-30T03:48:31.947Z
Hi R Bowles, and welcome to the site! There may not be enough information here to help. Can you edit to add more explanation (and possibly source code) to show what you're doing and where the problem is?
luser droog
CC BY-SA 4.0
2021-10-01T17:02:30.540Z
I have included code for the plot routine. As explained before, It plots the changing ADC values but presents it similar to a chart recorder in a continuous line. I want to be able to capture and plot the return from a piezo that is that is fired every 100 milliseconds with a duration of 200 microseconds.
R Bowles
CC BY-SA 4.0
2021-10-02T03:14:19.663Z
@R. Bowles Thank you. +1 I think there's a better chance you'll get an answer now.
luser droog
<2d><animation><line-drawing>
How to plot an ADC value from the Raspberry PI Pico using pythons matplotlib
R Bowles
# How to plot an ADC value from the Raspberry PI Pico using pythons matplotlib

(Asked by: username_0 on 2021-09-28)

Being new to Python and this community, I am trying to plot the pulse return from a transducer. The pulse return is 400 uSecs in duration and occurs every 100 milliseconds. So far I can plot the received values that are constantly being received and the plot graphs these in a strip chart fashion scrolling from right to left. I would like to be able to present the plot similar to how an oscilloscope would display the return pulse. I am not all that familiar with Numpy arrays yet. I am not sure what I am forgetting in writing the proper syntax for this. Here is a snippet of the code I am having issues with.

```
### Parameters   ##################
x = 300            # Number of points to display

y_range = [0,300]   # Range of possible Y values to display
#y_range = list(np.arange(0, 300))
xs = list(np.arange(0, 300))
ys = [0] * x
plot1.set_ylim(y_range)

###  Initialize communication with the pico and read value from ADC
reading = analogio.AnalogIn(board.ADC0)

###   Create plot line
line, = plot1.plot(xs, ys, color= "yellow")

###   Start Thread
def plot():
    def animate(i, ys):
    # Read data from Pico
        DATA = reading.value/20
    
    #   Add y to list
        ys.append(DATA)
    
    #   Limit y list to set number of items
        ys = ys[- x:]
        #ys = ys(np.arange(0, 300))
    
    #   Update line with new y values
        line.set_ydata(ys)
        print("ADC:", DATA) 
        return line,

### Set up plot to call animate() function
    ani = animation.FuncAnimation(fig, animate, fargs=(ys,),interval=.0001, blit=True)
    
    

print("thread starting")
t = threading.Thread(name='plot', target=plot)
t.setDaemon(True)
t.start()

```

**Tags:** 2d, animation, line-drawing

> **Comments:**
> **username_1**: Hi username_0, and welcome to the site! There may not be enough information here to help. Can you edit to add more explanation (and possibly source code) to show what you're doing and where the problem is?  
> **username_0**: I have included code for the plot routine. As explained before, It plots the changing ADC values but presents it similar to a chart recorder in a continuous line. I want to be able to capture and plot the return from a piezo that is that is fired every 100 milliseconds with a duration of 200 microseconds.  
> **username_1**: @R. username_0 Thank you. +1 I think there's a better chance you'll get an answer now.  

---

## 0 answers

