1

I'm trying to design a user interface for an app. The app will be using reliability and fitter to show the user some graphs.

Problems

Using reliability is difficult to show the graph on tkinter without having a mini figure popping out constantly as reliability use matplotlib.pyplot.

Next, the main problem, when plotting the graph in tkinter. I want it to change to another graph when I clicked on the button. But what I tried so far is pointless so far, the graph will be all stacked up. I have tried .get_tk_widget.delete() and methods mention in other posts but it won't remove the graph that is on tkinter. plt.clf() on remove the figure that pops up but not on the ones in tkinter.

Graph stacking

Here is what I have done

The below code is a simple version of what I trying to design. If anyone is interested, the full code is here.

import tkinter as tk
import pandas as pd
import numpy as np
import matplotlib.pyplot
import matplotlib.pyplot as plt 
matplotlib.use("TkAgg")

from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
from reliability.Fitters import Fit_Weibull_3P
from reliability.Probability_plotting import plot_points

main_data= pd.ExcelFile(r'C:\Users\Documents\All data.xlsx')
df= pd.read_excel(main_data, '124 deg' , names= ['RSR 100', 'BS'])
df1 = pd.read_excel(main_data, '154 deg', names= ['RSR 100', 'BS'])
df.index=np.arange(1,len(df['BS'])+1)
df1.index=np.arange(1,len(df1['BS'])+1)
data1= abs(df.loc[:, 'BS'].to_numpy())
data2= abs(df1.loc[:, 'BS'].to_numpy())

def plot_graph(): 
    fig = plt.figure(figsize = (5, 5), dpi = 100)
    fit_distributions = Fit_Weibull_3P(failures= data1, show_probability_plot= False, print_results= False)
    plt1 = fig.add_subplot(111) #subplot here is to make the pyplot to be embed on tkinter.
    fit_distributions.distribution.CDF(label='Fitted Distribution', color='Steelblue')
    plot_points(failures=data1, func='CDF', label='Failure data', color='red', alpha= 0.7)
    plt.legend() 
    canvas = FigureCanvasTkAgg(fig, master = window)  
    canvas.draw()
    canvas.get_tk_widget().pack()

def plot_next_graph():
    fig = plt.figure(figsize = (5, 5), dpi = 100)
    fit_distributions = Fit_Weibull_3P(failures= data2, show_probability_plot= False, print_results= False)
    plt1 = fig.add_subplot(111)
    fit_distributions.distribution.CDF(label='Fitted Distribution', color='Steelblue')
    plot_points(failures=data2, func='CDF', label='Failure data', color='red', alpha= 0.7)
    plt.legend()
    plt.close()
    canvas = FigureCanvasTkAgg(fig, master = window)  
    canvas.draw()
    canvas.get_tk_widget().pack()

window = tk.Tk()
window.title('Plotting in Tkinter')
window.geometry("500x500")
plot_button1 = ttk.Button(master = window, command = plot_graph, width = 15, text = "Plot 124 deg")
plot_button1.pack()
plot_button2 = ttk.Button(master = window, command = plot_next_graph, width = 15, text = "Plot 154 deg")
plot_button2.pack()
window.mainloop()

I am still a beginner in Python and tkinter, would appreciate if anyone would spend their time to help and guide me. Thank you.

Isaac98
  • 11
  • 4

0 Answers0