I try to make a line graph with python and the graph only appears a little in the end of the canvas in the GUI. The data that should be paper was taken from the database.
import sqlite3 ###----------------Connecting to the database-------------#####
DB = sqlite3.connect ("personal_project.db")
CURSOR = DB.cursor()
###----------------create the SQL command to create the table and save data-------------######
COMMAND1 = """CREATE TABLE IF NOT EXISTS
balance (
UserID INTEGER PRIMARY KEY,
Date TEXT,
Ammount TEXT,
Descriotion)"""
CURSOR.execute(COMMAND1)
from tkinter import *
from tkinter import messagebox
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
###----------------Create the window-------------#####
main_WINDOW = Tk()
main_WINDOW.title("Study App")
main_WINDOW.geometry("1940x1080")#width*length
main_WINDOW.configure(bg="#ffffff")
###------Show Information Using Graph-------###
graquery = '''SELECT Date, Ammount FROM balance'''
CURSOR.execute(graquery)
graresults = CURSOR.fetchall()
Date = [result[0] for result in graresults]
Ammount = [result[1] for result in graresults]
figure = plt.figure()
plt.plot(Date, Ammount)
plt.xlabel('Date')
plt.ylabel('Amount')
plt.title('Balance graph Graph')
gracanvas = Canvas(main_WINDOW, width=1070, height=452)
gracanvas.pack()
gracanvas.place(x=356, y=270)
figure_canvas = FigureCanvasTkAgg(figure, gracanvas)
gracanvas.create_window(0,0,window=figure_canvas.get_tk_widget())
CodePudding user response:
Consider this code:
gracanvas.create_window(0,0,window=figure_canvas.get_tk_widget())
The create_window
method by default centers the window at the given coordinate, which is what your image looks like. If you want the top-left corner of the window to be at 0,0, specify anchor="nw"
so that the northwest corner of the window is at the given coordinate.
gracanvas.create_window(0,0,window=figure_canvas.get_tk_widget(), anchor="nw")
# ^^^^^^^^^^^