plot sperate figures using tkinter class and matplotlib?

Viewed 18

I'm attempting to write a gui which will load multiple .csv files and allow me to plot the results separately and also in summary form by pressing a button for each. Here is an example .csv:

0,0,2.8,3.7
0,0,2.4,3.5
0,0,2.9,3.4
0,0,2.7,3.9

The code I'm using (cobbled together from various sources and modified by me):

from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
import tkinter as tk
import tkinter.ttk as ttk
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
from scipy.stats import norm

class Importing:
    def __init__(self, parent):
        self.parent = parent
        self.frame = tk.Frame(self.parent)
        self.frame2 = tk.Frame(self.frame,relief=tk.RAISED)
        self.b1 = tk.Button(self.frame, text = 'Load', width = 25, command = self.loading)
        self.b1.grid(row=0,column=0)
        self.b2 = tk.Button(self.frame, text = 'Plot Test A', width = 25, command = self.plot1)
        self.b2.grid(row=0,column=1)
        self.b3 = tk.Button(self.frame, text= 'Plot Test B', width = 25, command= self.plot2)
        self.b3.grid(row=0,column=2)
        self.b4 = tk.Button(self.frame, text= 'Plot Test C', width = 25, command= self.plot3)
        self.b4.grid(row=0,column=3)
        self.b5 = tk.Button(self.frame, text= 'Plot Summary', width = 25, command= self.summary)
        self.b5.grid(row=0,column=4)
        self.b6 = tk.Button(self.frame, text= 'Close', width = 25, command= self.close)
        self.b6.grid(row=0,column=5)
        self.frame.grid(row=0,column=0)
        self.frame2.grid(row=1,column=0)

    def loading(self):
        self.TestA = pd.read_csv('TestA.csv')
        self.TestB = pd.read_csv('TestB.csv')
        self.TestC = pd.read_csv('TestC.csv')
        return self.TestA,self.TestB,self.TestC

    def summary(self):
        fig, (ax1,ax2,ax3) = plt.subplots(3,1,sharex=False)
        mu1, std1 = norm.fit(self.TestA.iloc[:,2])     # Mask
        mu2, std2 = norm.fit(self.TestA.iloc[:,3])     # LDV
        mu3, std3 = norm.fit(self.TestB.iloc[:,2])     # Mask
        mu4, std4 = norm.fit(self.TestB.iloc[:,3])     # LDV
        mu5, std5 = norm.fit(self.TestC.iloc[:,2])     # Mask
        mu6, std6 = norm.fit(self.TestC.iloc[:,3])     # LDV
        ax1.hist(self.TestA.iloc[:,2], bins=10, density=True,alpha=0.6,edgecolor='k',color='r',label='exc')
        ax1.hist(self.TestA.iloc[:,3], bins=10, density=True,alpha=0.6,edgecolor='k',color='b',label='ldv')
        ax2.hist(self.TestB.iloc[:,2], bins=10, density=True,alpha=0.6,edgecolor='k',color='r',label='exc')
        ax2.hist(self.TestB.iloc[:,3], bins=10, density=True,alpha=0.6,edgecolor='k',color='b',label='ldv')
        ax3.hist(self.TestC.iloc[:,2], bins=10, density=True,alpha=0.6,edgecolor='k',color='r',label='exc')
        ax3.hist(self.TestC.iloc[:,3], bins=10, density=True,alpha=0.6,edgecolor='k',color='b',label='ldv')
        xmin, xmax = plt.xlim()
        x = np.linspace(xmin, xmax, 1000)
        p1 = norm.pdf(x, mu1, std1)
        p2 = norm.pdf(x, mu2, std2)
        p3 = norm.pdf(x, mu3, std3)
        p4 = norm.pdf(x, mu4, std4)
        p5 = norm.pdf(x, mu5, std5)
        p6 = norm.pdf(x, mu6, std6)
        ax1.plot(x, p1, 'k', linewidth=1)
        ax1.plot(x, p2, 'k', linewidth=1)
        ax1.set_title('Test A')
        ax1.set_xlabel('pressure [mbar]')
        ax1.set_ylabel('Frequency [ul]')
        ax1.legend()
        ax2.plot(x, p3, 'b', linewidth=1)
        ax2.plot(x, p4, 'b', linewidth=1)
        ax2.set_title('Test B')
        ax2.set_xlabel('pressure [mbar]')
        ax2.set_ylabel('Frequency [ul]')
        ax2.legend()
        ax3.plot(x, p5, 'r', linewidth=1)
        ax3.plot(x, p6, 'r', linewidth=1)
        ax3.set_title('Test C')
        ax3.set_xlabel('pressure [mbar]')
        ax3.set_ylabel('Frequency [ul]')
        ax3.legend()
        plt.tight_layout()
        
    def plot1(self):
        
        mu1, std1 = norm.fit(self.TestA.iloc[:,2])     # Mask
        mu2, std2 = norm.fit(self.TestA.iloc[:,3])     # LDV
        plt.hist(self.TestA.iloc[:,2], bins=10, density=True,alpha=0.6,edgecolor='k',color='r',label='exc')
        plt.hist(self.TestA.iloc[:,3], bins=10, density=True,alpha=0.6,edgecolor='k',color='b',label='ldv')
        xmin, xmax = plt.xlim()
        x = np.linspace(xmin, xmax, 1000)
        p1 = norm.pdf(x, mu1, std1)
        p2 = norm.pdf(x, mu2, std2)
        fig1 = plt.Figure()
        plt.title('Test A')
        plt.plot(x, p1, 'k', linewidth=1)
        plt.plot(x, p2, 'k', linewidth=1)
        plt.tight_layout()
        
    def plot2(self):
        
        mu3, std3 = norm.fit(self.TestB.iloc[:,2])     # Mask
        mu4, std4 = norm.fit(self.TestB.iloc[:,3])     # LDV
        plt.hist(self.TestB.iloc[:,2], bins=10, density=True,alpha=0.6,edgecolor='k',color='r',label='exc')
        plt.hist(self.TestB.iloc[:,3], bins=10, density=True,alpha=0.6,edgecolor='k',color='b',label='ldv')
        xmin, xmax = plt.xlim()
        x = np.linspace(xmin, xmax, 1000)
        p3 = norm.pdf(x, mu3, std3)
        p4 = norm.pdf(x, mu4, std4)
        fig2 = plt.Figure()
        plt.title('Test B')
        plt.plot(x, p3, 'b', linewidth=1)
        plt.plot(x, p4, 'b', linewidth=1)
        plt.tight_layout()
        
    def plot3(self):
        
        mu5, std5 = norm.fit(self.TestC.iloc[:,2])     # Mask
        mu6, std6 = norm.fit(self.TestC.iloc[:,3])     # LDV
        plt.hist(self.TestC.iloc[:,2], bins=10, density=True,alpha=0.6,edgecolor='k',color='r',label='exc')
        plt.hist(self.TestC.iloc[:,3], bins=10, density=True,alpha=0.6,edgecolor='k',color='b',label='ldv')
        xmin, xmax = plt.xlim()
        x = np.linspace(xmin, xmax, 1000)
        p5 = norm.pdf(x, mu5, std5)
        p6 = norm.pdf(x, mu6, std6)
        fig3 = plt.Figure()
        plt.plot(x, p5, 'r', linewidth=1)
        plt.plot(x, p6, 'r', linewidth=1)
        plt.tight_layout()
        
    def close(self):
        self.parent.destroy()

def main():
    root = tk.Tk()
    app = Importing(root)
    root.mainloop()

if __name__ == '__main__':
    main()

The code runs and mostly does what I want, however, if I plot 'plot1' I get a figure which I then have to close before I can plot another. Is it possible to have plots 1,2,3 & summary appear in seperate figure windows at the same time? Also, is there a better way to do this? Ideally I'd like these to open and close within the gui main window rather than separately outside the GUI.

Any advice is appreciated.

0 Answers
Related