Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Update plot
#1
HI all,
I am reading data from serial port to array and I want have one window with one button and two charts. If I click on the button, read serial function will run and then both charts will be updted.
Now I have plot and show and if I read data from serial, then one chart is open, the I have to close it and second is open and I have to close it and program stops.
I am very new in Python, I am sure this is very basic issue but I am lost.
Thanks for any advice.

MF
Reply
#2
You should post your code. Or we can randomly guess the source of your problem.
ndc85430 likes this post
Reply
#3
I am sorry, code here:

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='COM4', baudrate=2000000)
SAMPLES=20000
s = struct.Struct('<' + str(SAMPLES) + 'f')
T = 1.0 / 20000.0
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[1:20000] = unpacked_data[1:20000]
    ser.close

serial_read()

plt.plot(unpacked_data1[1000:3000])
plt.show()

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
#4
You can have more than one plot appear in a window. Look here in the documentation where it talks about working with multiple figures and axes.

https://matplotlib.org/stable/tutorials/...yplot.html

And here's an example of embedding a plot in a tkinter window.

https://matplotlib.org/stable/gallery/us...gskip.html

If you want a quick and dirty solution just make a script to make your two plots, then write a tkinter program that has a button which launches your plotting script as a subprocess.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,687 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  How to plot intraday data of several days in one plot mistermister 3 2,916 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  How to plot vertically stacked plot with same x-axis and SriMekala 0 1,931 Jun-12-2019, 03:31 PM
Last Post: SriMekala

Forum Jump:

User Panel Messages

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