[英]how to display matplotlib charts in tkinter
我試圖在 tkinter windows 中顯示 4 個或更多圖表,但它們放錯了位置,我認為我還需要一個滾動條。 這是代碼:
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()
我看到兩個問題
第一的:
您創建 4 個 canvas FigureCanvasTkAgg
並在每個 canvas 上使用add_subplot(222)
為 4 個地塊(2x2)創建位置,但在每個 ZFCC790C72A86190DE1ZDDC549D 中僅使用一個位置為此,您只能使用一個 canvas。
第二:
您需要pack(fill="both", expand=True)
來調整繪圖大小並使用 window 中的所有空間。
您還使用pack(side=tk.RIGHT)
可能會使布局出現問題
最少的工作代碼
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()
結果:
編輯:
與 4 個 canvas 相同 - 每個 canvas 使用add_plot('111')
只保留一個 plot - 但這次我使用grid()
而不是pack()
來組織它。
它需要columnconfigure
, rowconfigure
來調整單元格大小並使用 window 中的所有空間。 和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()
結果:
現在地塊的邊距更小。
使用 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.