大家好。我想知道每次更新图表时是否可以使用变量值设置新的字符串限制。下面是我的代码,我可以运行它并使用变量绘制新图形,但我的图例保留在数据中间,所以我不想更改图例位置然后我的想法是设置新的y na x轴限制以适合我的传奇。我会感谢任何帮助。
#!/usr/bin/python3
# Importando os pacotes
import sys
import time
import RPi.GPIO as GPIO
from tkinter import ttk
from tkinter import*
import tkinter as tk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
#Setting GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.IN)
GPIO.setup(23, GPIO.IN)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.output(17, GPIO.LOW)
GPIO.output(27, GPIO.LOW)
#Setting time count variables
t1fvm = 0.0
t1ivm = 0.0
mevm = 0.0
levm = 0
t2fvm = 0.0
t2ivm = 0.0
mdvm = 0.0
ldvm = 0
#Setting Bar chart variables
ptotal_1 = 20.0
ptotal_2 = 15.0
somatoria_1 = 3.0
somatoria_2 = 6.0
#Setting buttons functions
def lev():
if GPIO.input(17):
t1fvm = time.time()
global levm
global t1ivm
global mevm
n1mv = (t1fvm-t1ivm)
mevm = mevm+n1mv
levm = int(float(mevm))
GPIO.output(17, GPIO.LOW)
else:
t1ivm = time.time()
GPIO.output(17, GPIO.HIGH)
def ldv():
if GPIO.input(27):
t2fvm = time.time()
global ldvm
global t2ivm
global mdvm
n2mv = (t2fvm-t2ivm)
mdvm = mdvm+n2mv
ldvm = int(float(mdvm))
GPIO.output(27, GPIO.LOW)
else:
t2ivm = time.time()
GPIO.output(27, GPIO.HIGH)
class showAcionam(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "SmartLight")
container = tk.Frame(self)
container.pack(side = "top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
sairFrame = tk.Frame(self)
self.sairFrame = sairFrame
sairFrame.pack(side="bottom", fill="both", expand=True)
self.frames = {}
sair = ttk.Button(sairFrame, text="Sair")
menuu = ttk.Button(sairFrame, text="Menu",
command = lambda: self.show_frame(Menu))
menuu.pack(side="left", expand = True)
frame = Acionamento(container, self)
self.frames[Acionamento] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(Acionamento)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Acionamento(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = ttk.Label(self, text="Alarmes")
label.pack()
esquerdaFrame = tk.Frame(self)
self.esquerdaFrame = esquerdaFrame
esquerdaFrame.pack(side ="left", fill="both", expand=True, pady=50, padx=30)
direitaFrame = tk.Frame(self)
self.direitaFrame = direitaFrame
direitaFrame.pack(side ="left", fill="both", expand=True, pady=50, padx=30)
modoFrame = tk.Frame(direitaFrame)
self.direitaFrame.modoFrame = modoFrame
modoFrame.pack(side ="top", fill="both", expand=True, pady=15, padx=10)
setorFrame = tk.Frame(direitaFrame)
self.direitaFrame.setorFrame = setorFrame
setorFrame.pack(side ="top", fill="both", expand=True, pady=15, padx=10)
#- Liga e Desliga
onButton_1 = tk.Button(direitaFrame, text = "Button 1", height=2, width=6, command=lev)
onButton_1.pack(side="top", expand = True)
#- Liga e Desliga
onButton_2 = tk.Button(direitaFrame, text = "Button 2", height=2, width=6, command=ldv)
onButton_2.pack(side="top", expand = True)
#Defining tkinter chart
f = Figure(figsize=(4, 3), dpi=100)
modo= f.add_subplot(111)
self.modo = modo
width = 0.5
grafico_operacao = modo.bar(ptotal_1, somatoria_1, width, color='r')
self.grafico_operacao = grafico_operacao
grafico_operacao1 = modo.bar(ptotal_2+width, somatoria_2, color='g')
self.grafico_operacao1 = grafico_operacao1
modo.set_xlabel('Tempo (H)')
modo.set_ylabel('Energia Consumida (KW.H)')
modo.set_title('Consumo por Modo de Operação')
modo.legend((grafico_operacao[0], grafico_operacao1[0]),('1', '2'))
canvas = FigureCanvasTkAgg(f, modoFrame)
self.canvas = canvas
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)
toolbar = NavigationToolbar2TkAgg(canvas, modoFrame)
toolbar.update()
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
## Functio which should write new graph with new values. Here I want to set new x and y axis min and max values. How can I perform it using variables?
def Grafico():
global somatoria_1
global somatoria_2
global ptotal_1
global ptotal_2
ptotal_1 = (ptotal_1*somatoria_1)
ptotal_2 = (ptotal_2*somatoria_2)
somatoria_1 = (levm/3600)
somatoria_2 = (ldvm/3600)
modo.clear()
width = 0.5
grafico_operacao = modo.bar(ptotal_1, somatoria_1, width, color='r')
self.grafico_operacao = grafico_operacao
grafico_operacao1 = modo.bar(ptotal_2+width, somatoria_2, color='g')
self.grafico_operacao1 = grafico_operacao1
modo.set_xlabel('Tempo (H)')
modo.set_ylabel('Energia Consumida (KW.H)')
modo.set_title('Consumo por Modo de Operação')
modo.legend((grafico_operacao[0], grafico_operacao1[0]),('1', '2'))
canvas.show()
#-Atualizar
atualizarv = tk.Button(esquerdaFrame, text = "Atualizar", height=2, width=6, command=Grafico)
atualizarv.pack(side="top", expand = True)
app = showAcionam()
app.geometry("800x600")
app.mainloop()