Home > Net >  Tkinter don't display line chart
Tkinter don't display line chart

Time:05-16

in the calc def I want to display all of the for cycle (picture by picture) on the gui with the draw def, but only the last one is showed. I tried it 2 different way but none of them worked. I guess the problem is that I give them 1 place and all of them is showed on that place in the top of each other. How can I solve that? Thanks in advance.

from tkinter import *
from tkinter import messagebox

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends._backend_tk import NavigationToolbar2Tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from pandas import DataFrame
from decimal import *

root = Tk()
root.geometry('800x300')
root.title('PythonExamples.org - Tkinter Example')

global e1
global numm
global my_entry
my_entry= Entry(root)
e1=Entry(root)
e1.place(x=100,y=180)
korok=Entry(root)
korok.place(x=100,y=210)
entries=[]
entries2=[]
new_array=[]


def calc(numbers):
    n=int(korok.get())
    P=np.dot(numbers,numbers)
    for i in range(n):
        P=np.dot(P,numbers)
        draw(P)
        np.set_printoptions(precision=3)
        print(P)

    label = Label(root, text=str(P),font=("Arial", 15)).place(x=20, y=60)

def draw(data):
    fig,a = plt.subplots()
    df2 = DataFrame(data)
    figure2 = plt.Figure(figsize=(5, 5), dpi=50)
    ax2 = figure2.add_subplot(111)
    line3 = FigureCanvasTkAgg(figure2, root)
    line3.get_tk_widget().place(x=300,y=100)
    #line3.get_tk_widget().grid(row=5, column=5)
    df2.plot(kind='line', legend=True, ax=ax2, fontsize=10)
    plt.close(fig)
    ax2.set_title('Markov')
def create():
    numm = int(e1.get())
    global my_entry
    for x in range(numm):
        row = []
        for i in range(numm):
            my_entry = Entry(root)
            my_entry.grid(row=x, column=i)
            row.append(my_entry)
        entries.append(row)


def save():
    my_array = [[float(el.get()) for el in row] for row in entries]
    new_array = np.asarray(my_array)

    calc(new_array)


create = Button(root,text='Submit',command=create).place(x=40,y=180)
save = Button(root,text='Szamol',command=save).place(x=40,y=210)

my_label=Label(root,text='')
root.mainloop()

CodePudding user response:

The first thing to understand is the concept of figure and axes in matplotlib

Figure is like a sheet, you can draw many things on it plt.subplots function creates spaces where you can draw on a figure.

Axes is where you can show your data with X and Y

Main error : on draw function, you create a new figure every time, that is every figure is drawn on top of each other. Create figure once (in calc) then draw on different axes with draw function.

Here is some corrections I made, suit them in your program

def calc(numbers):
    n=int(korok.get())
    P=np.dot(numbers,numbers)
    fig, a = plt.subplots(n, 1) # Create n row of 1 plot in fig
    for i in range(n):
        P=np.dot(P,numbers)
        draw(P, fig, a[i]) # specify which axis where show P
        np.set_printoptions(precision=3)
        print(P)

Here, you can use plt.subplots(n_rows, n_columns) function where n_rows is the number of rows n_columns of columns.

A lot more changes in draw

def draw(data, figure, ax):
    df2 = DataFrame(data)
    line3 = FigureCanvasTkAgg(figure, root)
    line3.get_tk_widget().place(x=300,y=100)
    #line3.get_tk_widget().grid(row=5, column=5)
    df2.plot(kind='line', legend=True, ax=ax, fontsize=10) # ploting on ax axis space
    plt.close(figure)
    ax.set_title('Markov') # Specify the title of ax axis
  • Related