Nadjib Rahmani
Nadjib Rahmani

Reputation: 29

how to display matplotlib charts in tkinter

i am trying to display 4 or more charts in tkinter windows but they are misplaced and i thinks i need a scroll bar also . this is the code :

root = tk.Tk()

    figure1 = plt.Figure(figsize=(2,2), dpi=100)

    ax1 = figure1.add_subplot(221)
    ax1.plot(df1['year'], df1['personal'], color='red')
    scatter1 = FigureCanvasTkAgg(figure1, root)
    scatter1.get_tk_widget().pack()
    ax1.legend([''])
    ax1.set_xlabel('valeur de personals')
    ax1.set_title('ev de personal ')

    figure2 = plt.Figure(figsize=(2,2), dpi=100)
    ax2 = figure2.add_subplot(222)
    scatter2 = FigureCanvasTkAgg(figure2, root)
    scatter2.get_tk_widget().pack(side=tk.RIGHT)
    ax2.legend([''])
    ax2.set_xlabel('valeur BSA')
    ax2.set_title('Evolutiion des valeurs BSA depuis 1990 ')
    ax2.plot(df2['year'], df2['value'], color='red')

    figure3 = plt.Figure(figsize=(2,2), dpi=100)
    ax3 = figure3.add_subplot(223)
    #the same code for the reste 


    root.mainloop()

but this is the result : charts misplaced graph

Upvotes: 2

Views: 5360

Answers (1)

furas
furas

Reputation: 143231

I see two problems

First:

You create 4 canvas FigureCanvasTkAgg and on every canvas you use add_subplot(222) to create places for 4 plots (2x2) but you use only one place in every canvas. You could use only one canvas for this.

Second:

You need pack(fill="both", expand=True) to resize plots and use all space in window.

You also use pack(side=tk.RIGHT) which can make problem with layout


Minimal working code

import tkinter as tk
import pandas as pd
import matplotlib.pyplot as plt

import matplotlib
matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
#from matplotlib.figure import Figure

df1 = pd.DataFrame({
    'year': [2001, 2002, 2003],
    'value': [1, 3, 2],
    'personal': [9, 1, 5],
})

df2 = pd.DataFrame({
    'year': [2001, 2002, 2003],
    'value': [1, 3, 2],
    'personal': [9, 1, 5],
})

# --- 

root = tk.Tk()

figure = plt.Figure(figsize=(2,2), dpi=100)

scatter = FigureCanvasTkAgg(figure, root)
scatter.get_tk_widget().pack() #fill='both', expand=True)

# --- 

ax1 = figure.add_subplot(221)
ax1.plot(df1['year'], df1['personal'], color='red')

ax1.legend([''])
ax1.set_xlabel('valeur de personals')
ax1.set_title('ev de personal ')

# --- 

ax2 = figure.add_subplot(222)
ax2.plot(df2['year'], df2['value'], color='red')

ax2.legend([''])
ax2.set_xlabel('valeur BSA')
ax2.set_title('Evolutiion des valeurs BSA depuis 1990 ')

# --- 

ax3 = figure.add_subplot(223)
ax3.plot(df1['year'], df1['personal'], color='red')

ax3.legend([''])
ax3.set_xlabel('valeur de personals')
ax3.set_title('ev de personal ')

# --- 

ax4 = figure.add_subplot(224)
ax4.plot(df2['year'], df2['value'], color='red')

ax4.legend([''])
ax4.set_xlabel('valeur BSA')
ax4.set_title('Evolutiion des valeurs BSA depuis 1990 ')

# ---

root.mainloop()

Result:

enter image description here


EDIT:

The same with 4 canvas - and every canvas keep only one plot using add_plot('111') - but this time I use grid() instead of pack() to organize it.

It needs columnconfigure, rowconfigure to resize cells and use all space in window. And grid( ..., sticky='news') to resize canvas to cell's size.

import tkinter as tk
import pandas as pd
import matplotlib.pyplot as plt

import matplotlib
matplotlib.use("TkAgg")

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
#from matplotlib.figure import Figure

df1 = pd.DataFrame({
    'year': [2001, 2002, 2003],
    'value': [1, 3, 2],
    'personal': [9, 1, 5],
})

df2 = pd.DataFrame({
    'year': [2001, 2002, 2003],
    'value': [1, 3, 2],
    'personal': [9, 1, 5],
})

# --- 

root = tk.Tk()

# resize grid
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=1)
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)

# --- 

figure1 = plt.Figure(figsize=(2,2), dpi=100)

scatter1 = FigureCanvasTkAgg(figure1, root)
scatter1.get_tk_widget().grid(row=0, column=0, sticky='news')
#scatter1.get_tk_widget().pack(fill='both', expand=True)

ax1 = figure1.add_subplot(111)
ax1.plot(df1['year'], df1['personal'], color='red')

ax1.legend([''])
ax1.set_xlabel('valeur de personals')
ax1.set_title('ev de personal ')

# --- 

figure2 = plt.Figure(figsize=(2,2), dpi=100)

scatter2 = FigureCanvasTkAgg(figure2, root)
scatter2.get_tk_widget().grid(row=0, column=1, sticky='news')
#scatter2.get_tk_widget().pack(side='right', fill='both', expand=True)

ax2 = figure2.add_subplot(111)
ax2.plot(df2['year'], df2['value'], color='red')

ax2.legend([''])
ax2.set_xlabel('valeur BSA')
ax2.set_title('Evolutiion des valeurs BSA depuis 1990 ')

# --- 

figure3 = plt.Figure(figsize=(2,2), dpi=100)

scatter3 = FigureCanvasTkAgg(figure3, root)
scatter3.get_tk_widget().grid(row=1, column=0, sticky='news')
#scatter3.get_tk_widget().pack(fill='both', expand=True)

ax3 = figure3.add_subplot(111)
ax3.plot(df1['year'], df1['personal'], color='red')

ax3.legend([''])
ax3.set_xlabel('valeur de personals')
ax3.set_title('ev de personal ')

# --- 

figure4 = plt.Figure(figsize=(2,2), dpi=100)

scatter4 = FigureCanvasTkAgg(figure4, root)
scatter4.get_tk_widget().grid(row=1, column=1, sticky='news')
#scatter4.get_tk_widget().pack(fill='both', expand=True)

ax4 = figure4.add_subplot(111)
ax4.plot(df2['year'], df2['value'], color='red')

ax4.legend([''])
ax4.set_xlabel('valeur BSA')
ax4.set_title('Evolutiion des valeurs BSA depuis 1990 ')

# ---

root.mainloop()

Result:

Now plots has smaller margins.

enter image description here

Upvotes: 2

Related Questions