如何在 Tkinter 的 window 中更改具有 Figure 的子图参数?例如我想添加 xlabel 和 ylabel

How do I change the subplot parameters having a Figure in a window in Tkinter ? As for example I want to add the xlabel and ylabel

我有一个应用程序应该从传感器获取数据到实时图表中,这是一个添加到图中的子图。 添加子图后我现在遇到一个问题,我不知道如何将绘图参数更改为 xlabel、ylabel。如果我导入 plt 这会起作用,但如果我导入将进一步添加到 Tkinter 中的 window 的图形则不起作用。

#file livegraph.py

import matplotlib.animation as animation
import datetime

#this is a draft for the liveGraph class
#the objective is to get live data from a sensor 

class liveGraph:

    #by default define the interval as being 1000 mSec
    intervalAnim = 1000

    def __init__(self,fig):
        self.xax = 0
        self.xs = []
        self.ys = []

        self.ax = fig.add_subplot(111)
        self.ax.set_xlabel('teeeest')
        #fig.title('Graph test')
        #fig.set_xlabel("Time")
        #fig.ylabel("% SMS")

        self.anim = animation.FuncAnimation(fig, self.animate, interval = self.intervalAnim)

    def animate(self,i):

        self.xs.append(self.xax)
        self.ys.append(datetime.datetime.now().second)
        self.xax+=1

        self.ax.clear()
        self.ax.plot(self.xs,self.ys)

        if self.xax > 90:
            self.anim.event_source.stop()



from tkinter import *
from matplotlib import style
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from livegraph import liveGraph

# Define the main_screen as a tkinter
app_screen = Tk()  # create a GUI window
app_screen.geometry("1920x1080")  # set the configuration of GUI window
app_screen.resizable(width=True,height=True)
app_screen.title("Testare izolator")  # set the title of GUI window

style.use('bmh')

#figure represents the graphic part of the system
figure = Figure(figsize=(10, 5), facecolor='white',frameon=True)
figure.suptitle('This is the figure title', fontsize=12)
#figure.add_gridspec(10,10)

#this are some parameters that I can easily change if I am using plt
# plt.title('Graph test')
#plt.xlabel("Time")
#plt.ylabel("% SMS")


#x = [ 0, 1, 2, 3, 4 ]
#y = [ 0, 1, 2, 3, 4 ]
#lines = plt.plot(x, y)
#plt.grid()
#plt.axis([0,10,0,10])
#plt.setp(lines, color= "b")

canvas = FigureCanvasTkAgg(figure, app_screen)
canvas.get_tk_widget().pack(side=TOP, anchor =NW, padx=100, pady=10)

newAnimation = liveGraph(figure)

app_screen.mainloop()  # start the GUI

你应该使用self.ax.来添加元素

    self.ax.set_xlabel('teeeest')
    self.ax.set_title('Graph test')
    self.ax.set_xlabel("Time")
    self.ax.set_ylabel("% SMS")

但是还有其他问题,因为 self.ax.clear() 删除了这些元素。


第一种方法:

如果你使用 self.ax.clear() 然后你删除标签,你必须一次又一次地放置标签

def animate(self, i):

    self.xs.append(self.xax)
    #self.ys.append(datetime.datetime.now().second)
    self.ys.append(random.randint(0, 10))
    self.xax += 1

    self.ax.clear()
    self.ax.plot(self.xs,self.ys)
    if self.xax > 90:
        self.anim.event_source.stop()

    self.ax.set_xlabel('teeeest')
    self.ax.set_title('Graph test')
    self.ax.set_xlabel("Time")
    self.ax.set_ylabel("% SMS")

第二种方法:

要仅在必须删除 self.ax.clear() 后才添加元素,而不是 plot(),您应该在 `init

中创建空图
    self.ax = fig.add_subplot(111)
    self.ax.set_xlabel('teeeest')
    self.ax.set_title('Graph test')
    self.ax.set_xlabel("Time")
    self.ax.set_ylabel("% SMS")

    self.line, = self.ax.plot([], [])

并在动画中使用 set_data() 更新现有绘图中的数据

    self.line.set_data(self.xs, self.ys)

但它不会重新缩放绘图,您必须手动执行(如果您想重新缩放)

    self.ax.relim()  # recalculate limits
    self.ax.autoscale_view(True,True,True)  # rescale using limits

第一种方法

的完整代码
import matplotlib.animation as animation
import datetime
import random
#this is a draft for the liveGraph class
#the objective is to get live data from a sensor 

class liveGraph:

    #by default define the interval as being 1000 mSec
    intervalAnim = 1000

    def __init__(self, fig):
        self.xax = 0
        self.xs = []
        self.ys = []

        self.ax = fig.add_subplot(111)
        self.ax.set_xlabel('teeeest')

        self.anim = animation.FuncAnimation(fig, self.animate, interval=self.intervalAnim)

    def animate(self, i):

        self.xs.append(self.xax)
        #self.ys.append(datetime.datetime.now().second)
        self.ys.append(random.randint(0, 10))
        self.xax += 1

        self.ax.clear()
        self.ax.plot(self.xs,self.ys)
        if self.xax > 90:
            self.anim.event_source.stop()

        self.ax.set_title('Graph test')
        self.ax.set_xlabel("Time")
        self.ax.set_ylabel("% SMS")

from tkinter import *
from matplotlib import style
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
#from livegraph import liveGraph

# Define the main_screen as a tkinter
app_screen = Tk()  # create a GUI window
app_screen.geometry("1920x1080")  # set the configuration of GUI window
app_screen.resizable(width=True, height=True)
app_screen.title("Testare izolator")  # set the title of GUI window

style.use('bmh')

#figure represents the graphic part of the system
figure = Figure(figsize=(10, 5), facecolor='white', frameon=True)
figure.suptitle('This is the figure title', fontsize=12)
#figure.add_gridspec(10,10)

#this are some parameters that I can easily change if I am using plt
#plt.title('Graph test')
#plt.xlabel("Time")
#plt.ylabel("% SMS")


#x = [ 0, 1, 2, 3, 4 ]
#y = [ 0, 1, 2, 3, 4 ]
#lines = plt.plot(x, y)
#plt.grid()
#plt.axis([0,10,0,10])
#plt.setp(lines, color= "b")

canvas = FigureCanvasTkAgg(figure, app_screen)
canvas.get_tk_widget().pack(side=TOP, anchor=NW, padx=100, pady=10)

newAnimation = liveGraph(figure)

#canvas.draw()

app_screen.mainloop()  # start the GUI

第二种方法

的完整代码
import matplotlib.animation as animation
import datetime
import random
#this is a draft for the liveGraph class
#the objective is to get live data from a sensor 

class liveGraph:

    #by default define the interval as being 1000 mSec
    intervalAnim = 1000

    def __init__(self, fig):
        self.xax = 0
        self.xs = []
        self.ys = []

        self.ax = fig.add_subplot(111)
        self.ax.set_xlabel('teeeest')
        self.ax.set_title('Graph test')
        self.ax.set_xlabel("Time")
        self.ax.set_ylabel("% SMS")

        # create empty plot at start    
        self.line, = self.ax.plot([], [])

        self.anim = animation.FuncAnimation(fig, self.animate, interval=self.intervalAnim)

    def animate(self, i):

        self.xs.append(self.xax)
        #self.ys.append(datetime.datetime.now().second)
        self.ys.append(random.randint(0, 2))
        self.xax += 1

        # update data in existing plot
        self.line.set_data(self.xs, self.ys)

        # rescale plot (if you need it)
        self.ax.relim()  # recalculate limits
        self.ax.autoscale_view(True,True,True)  # rescale using limits

        if self.xax > 90:
            self.anim.event_source.stop()

from tkinter import *
from matplotlib import style
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
#from livegraph import liveGraph

# Define the main_screen as a tkinter
app_screen = Tk()  # create a GUI window
app_screen.geometry("1920x1080")  # set the configuration of GUI window
app_screen.resizable(width=True, height=True)
app_screen.title("Testare izolator")  # set the title of GUI window

style.use('bmh')

#figure represents the graphic part of the system
figure = Figure(figsize=(10, 5), facecolor='white', frameon=True)
figure.suptitle('This is the figure title', fontsize=12)
#figure.add_gridspec(10,10)

#this are some parameters that I can easily change if I am using plt
#plt.title('Graph test')
#plt.xlabel("Time")
#plt.ylabel("% SMS")


#x = [ 0, 1, 2, 3, 4 ]
#y = [ 0, 1, 2, 3, 4 ]
#lines = plt.plot(x, y)
#plt.grid()
#plt.axis([0,10,0,10])
#plt.setp(lines, color= "b")

canvas = FigureCanvasTkAgg(figure, app_screen)
canvas.get_tk_widget().pack(side=TOP, anchor=NW, padx=100, pady=10)

newAnimation = liveGraph(figure)

#canvas.draw()

app_screen.mainloop()  # start the GUI