Python Forum
Matplotlib - automatic update
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Matplotlib - automatic update
#1
Hi all,
I am reading data from serial and then plot them. Everytime I wan to read from serial I send a char and get required data.
I want to change my code to update both plots automatically.
1) Send char to serial
2) Read 30k samples to array
3) Plot data (time signal and FFT)
4) Send char to serial
5) Read 30k samples to array
6) Update plots with new data without close plot windows

I am thinking about and the best solution for me is to have one window with one plot and 2 buttons to change the auto-update plot.

Could you help me with this please? Thanks!

This is my code:

import serial
import struct
import matplotlib.pyplot as plt
import scipy.fft
import numpy as np
from scipy.fftpack import fft

ser = serial.Serial(port='COM8', baudrate=2000000)
SAMPLES=30000
SPS = 15000
s = struct.Struct('<' + str(SAMPLES) + 'f')
T = 1.0 / SPS
unpacked_data1 = []

def serial_read():
    ser.reset_input_buffer()
    ser.write("r".encode())
    serial_data = ser.read(SAMPLES*4)
    unpacked_data = s.unpack(serial_data)
    unpacked_data1[0:SAMPLES] = unpacked_data[0:SAMPLES]
    ser.close

serial_read()
plt.plot(unpacked_data1[0:SAMPLES])

x = np.linspace(0.0, SAMPLES*T, SAMPLES)
y = unpacked_data1
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), SAMPLES//2)
fig, ax = plt.subplots()
ax.plot(xf, 2.0/SAMPLES * np.abs(yf[:SAMPLES//2]))

plt.show()  
Reply
#2
Does your existing code work for making 1 plot?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 275 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,015 Jan-21-2023, 11:33 PM
Last Post: SamLiu
  automatic create folders Mr_Kool 4 1,717 Dec-21-2021, 04:38 PM
Last Post: BashBedlam
  Automatic user/password entry on prompt by bash script PBOX_XS4_2001 3 2,727 May-18-2021, 06:42 PM
Last Post: Skaperen
  Matplotlib: How do I convert Dates from Excel to use in Matplotlib JaneTan 1 3,161 Mar-11-2021, 10:52 AM
Last Post: buran
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,626 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Automatic registering python to registry kozaizsvemira 1 2,142 Oct-22-2019, 11:23 AM
Last Post: kozaizsvemira
  Automatic redefining Hassediagram 5 3,217 Feb-25-2019, 01:36 PM
Last Post: Hassediagram
  Idea of timelapse with automatic settings hhanswurster 2 3,193 May-26-2018, 01:43 PM
Last Post: killerrex
  python to install printer drivers automatic leoxucn 4 7,588 Jul-10-2017, 09:21 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020