facing issues while plotting a graph in matplotlib

Viewed 56

I'm trying to make a python project which is an employee management system where i'm using various libraries of python like tkinter, sqlite3 (to connect the database), matplotlib. In the proposed project, we can add, view, update, delete and plot graphs for the employees. The issues i'm facing are towards the graphs. The code for the graphs is as follows

from tkinter import *
from tkinter.messagebox import *
from tkinter.scrolledtext import *
from sqlite3 import *
import matplotlib.pyplot as plt

def graphback():
    graph_window.deiconify()
    main_window.withdraw

def graph():
    con = None
    try:
        con = connect(project.db)
        cursor = con.cursor()
        sql = "select * from employee"
        cursor.execute(sql)
        data = cursor.fetchall()
        name = []
        salary = []
        plt.plot(name, salary)
        plt.title("Highest Salaried Employee")
        plt.show
    except Exception as e:
        showerror("issue ", e)
        con.rollback()
    finally:
        if con is not None:
            con.close()
    

def mgraph():
    main_window.deiconify()
    graph_window.withdraw()

I can't get the graph when i run the code. What should i do to run the code?

1 Answers

You missed the parenthesis around plt.show, otherwise you're not calling the function.

Change it to

from tkinter import *
from tkinter.messagebox import *
from tkinter.scrolledtext import *
from sqlite3 import *
import matplotlib.pyplot as plt

def graphback():
    graph_window.deiconify()
    main_window.withdraw

def graph():
    con = None
    try:
        con = connect(project.db)
        cursor = con.cursor()
        sql = "select * from employee"
        cursor.execute(sql)
        data = cursor.fetchall()
        name = []
        salary = []
        plt.plot(name, salary)
        plt.title("Highest Salaried Employee")
        # SEE HERE
        plt.show()
    except Exception as e:
        showerror("issue ", e)
        con.rollback()
    finally:
        if con is not None:
            con.close()
    

def mgraph():
    main_window.deiconify()
    graph_window.withdraw()
Related