Python Forum
Update plot - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Update plot (/thread-36202.html)



Update plot - frohr - Jan-27-2022

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


RE: Update plot - deanhystad - Jan-27-2022

You should post your code. Or we can randomly guess the source of your problem.


RE: Update plot - frohr - Jan-27-2022

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



RE: Update plot - deanhystad - Jan-27-2022

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/introductory/pyplot.html

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

https://matplotlib.org/stable/gallery/user_interfaces/embedding_in_tk_sgskip.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.