在 calc def 中,我想用 draw def 在 gui 上顯示所有的 for 回圈(逐張圖片),但只顯示最后一個。我嘗試了兩種不同的方式,但都沒有奏效。我想問題是我給了他們 1 個位置,并且所有這些都顯示在彼此頂部的那個位置上。我該如何解決?提前致謝。
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()
uj5u.com熱心網友回復:
首先要了解的是figure和axes的概念matplotlib
圖就像一張紙,你可以在上面畫很多東西,
plt.subplots功能創建了可以在圖上畫畫的空間。
軸是您可以使用 X 和 Y 顯示資料的地方
主要錯誤:在draw函式上,您每次都創建一個新圖形,即每個圖形都繪制在彼此之上。創建一次圖形(在 中),然后使用函式calc在不同的軸上繪制。draw
這是我所做的一些更正,適合您的程式
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)
在這里,您可以使用plt.subplots(n_rows, n_columns)函式 wheren_rows是n_columns列的行數。
還有很多變化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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/475792.html
