0

am trying to figure out how to clear the axis in readiness for new plotting, I have tried ax.clf(), fig.clf() but nothing is happening. where am I not doing well? at the moment am not getting any errors and am using Matplotlib vers. 3.4.3.

from tkinter import *
import matplotlib.pyplot as plt
import numpy as np
import time
import datetime
import mysql.connector
import matplotlib.dates as mdates


my_connect = mysql.connector.connect(host="localhost", user="Kennedy", passwd="Kennerdol05071994", database="ecg_db", auth_plugin="mysql_native_password")
mycursor = my_connect.cursor()

voltage_container = []
time_container = []


def analyze_voltage_time():
    global ax, fig
    pat_id = 1
    query = "SELECT voltage, time FROM ecg_data_tbl where patient_id =  " +str(pat_id)
    mycursor.execute(query)
    result = mycursor .fetchall()
    voltage, time = list(zip(*result))
    for volts in voltage:
        voltage_container.append(volts)
    for tim in time:
        time_container.append(str(tim))    

    fig = plt.figure(1, figsize = (15, 6), dpi = 80, constrained_layout = True)
    ax = fig.add_subplot()
    ax.plot(time_container, voltage_container)
    for label in ax.get_xticklabels():
        label.set_rotation(40)
        label.set_horizontalalignment('right')
    ax.set_title("Electrocadiogram")
    ax.set_xlabel("Time(hh:mm:ss)")
    ax.set_ylabel("Voltage(mV)")
    ax.grid(b=True, which='major', color='#666666', linestyle='-')
    ax.minorticks_on()
    ax.grid(b=True, which='minor', color='#666666', linestyle='-', alpha=0.2)
    plt.show()


def clear_():
    ax.cla()
    fig.clf()


# =================================MAIN GUI WINDOW======================================
analysis_window = Tk()
analysis_window.configure(background='light blue')
analysis_window.iconbitmap('lardmon_icon.ico')
analysis_window.title("ECG-LArdmon - ANALYZER")
analysis_window.geometry('400x200')
analysis_window.resizable(width=False, height=False)


# ===========================BUTTONS===================================
analyse_btn = Button(analysis_window, text='analyze', width = 20, command=analyze_voltage_time)
analyse_btn.pack()

clear_btn = Button(analysis_window, text= 'clear', width = 20, command=clear_)
clear_btn.pack()    


analysis_window.mainloop()
Kennerdol
  • 119
  • 3
  • 11
  • 1
    Does this answer your question? [How to update a graph created by matplotlib in tkinter](https://stackoverflow.com/questions/59001195/how-to-update-a-graph-created-by-matplotlib-in-tkinter) – Henry Yik Oct 31 '21 at 08:13
  • As show in the link Henry Yik provided, did you try `fig.clear()`? – Sylvester Kruin Oct 31 '21 at 14:32
  • @HenryYik I'm just from trying but my code is behaving strangely am sure there is something not okay with it. – Kennerdol Oct 31 '21 at 17:46
  • In my opinion, a stand alone ``ax.cla()`` is the simplest approach – Karina Nov 02 '21 at 08:06

0 Answers0