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()
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:
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.
use tkchart: https://pypi.org/project/tkchart/
import tkchart
import tkinter
root = tkinter.Tk()
#create chart
chart_1 = tkchart.LineChart(master=root,width=1000 ,height=600 ,chart_line_len=100 ,sections=True ,sections_count=10 ,values_labels=True ,values_labels_count=5 ,
max_value = 100)
chart_1.pack()
#create lines for chart
line_1 = tkchart.Line(master=chart_1 ,color="#00ff00" ,height=4)
line_2 = tkchart.Line(master=chart_1 ,height=4 ,color ="#ffff00")
value = [x for x in range(0,100)]
import random
def display():
chart_1.display(line=line_1 ,values=random.choices(value))
root.after(500,display)
display()
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.