[英]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.我试图在 tkinter windows 中显示 4 个或更多图表,但它们放错了位置,我认为我还需要一个滚动条。 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.您创建 4 个 canvas
FigureCanvasTkAgg
并在每个 canvas 上使用add_subplot(222)
为 4 个地块(2x2)创建位置,但在每个 ZFCC790C72A86190DE1ZDDC549D 中仅使用一个位置You could use only one canvas for this.为此,您只能使用一个 canvas。
Second:第二:
You need pack(fill="both", expand=True)
to resize plots and use all space in window.您需要
pack(fill="both", expand=True)
来调整绘图大小并使用 window 中的所有空间。
You also use pack(side=tk.RIGHT)
which can make problem with layout您还使用
pack(side=tk.RIGHT)
可能会使布局出现问题
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.与 4 个 canvas 相同 - 每个 canvas 使用
add_plot('111')
只保留一个 plot - 但这次我使用grid()
而不是pack()
来组织它。
It needs columnconfigure
, rowconfigure
to resize cells and use all space in window.它需要
columnconfigure
, rowconfigure
来调整单元格大小并使用 window 中的所有空间。 And grid(..., sticky='news')
to resize canvas to cell's size.和
grid(..., sticky='news')
将 canvas 调整为单元格的大小。
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/使用 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.