【问题标题】:facing issues while plotting a graph in matplotlib在 matplotlib 中绘制图形时遇到问题
【发布时间】:2021-12-03 15:57:06
【问题描述】:

我正在尝试制作一个 python 项目,它是一个员工管理系统,我在其中使用各种 python 库,如 tkinter、sqlite3(连接数据库)、matplotlib。在提议的项目中,我们可以为员工添加、查看、更新、删除和绘制图表。我面临的问题是图表。图表代码如下

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

我在运行代码时无法获取图表。我应该怎么做才能运行代码?

【问题讨论】:

  • 您可能想查看official docs,了解如何将matplotlib 嵌入tkinter

标签: python sqlite matplotlib tkinter


【解决方案1】:

您错过了plt.show 周围的括号,否则您不会调用该函数。

改成

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

【讨论】:

  • 感谢您的回答。我已经按照建议添加了括号,现在没有这样的错误,但我似乎无法获得代码的图表。它运行但不显示图表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-22
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 2020-06-20
相关资源
最近更新 更多