使用 matplotlib 在 python 上绘图

Plotting on python with matplotlib

我实际上正在做一个学校项目。我们需要读取并显示来自 4 个不同传感器的信号。我决定使用 python 而不是 matlab (simulink) 来实现它,因为 matlab 无法识别 raspberry pi 3. 我已经制作了一个非常简单的指南 whit Tkinter,它不断读取和显示来自传感器的值并将它在图表中。问题是,当我用代码来绘制图表时,它会停止传感器的刷新,并在一个图中显示四个值。问题是(是):

如何在四个不同的图表中显示四个值?

如何使刷新自动进行?

如何一键关闭所有数字?

如果可能的话,您能告诉我在哪里可以找到使图形更直观的信息吗?如何自定义图表。

这是我的代码:

#IMPORTAR LIBRERIAS NECESARIAS
from Tkinter import *
from drawnow import *
import RPi.GPIO as GPIO
import matplotlib.pyplot as plt
import numpy
import tkFont
import time
import Adafruit_DHT

#Variables
trig = 16
echo = 18
pir = 15
distance = 0
distancia = 0
tiempo = 0
temp = 0
humd = 0
distanciaM = []
temperaturaM = []
presenciaM = []
humedadM = []

#DECLARAR ENTRADAS Y SALIDAS
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(trig, GPIO.OUT)
GPIO.setup(echo, GPIO.IN)
GPIO.setup(pir, GPIO.IN)


#VENTANAS
win = Tk()
plt.figure()

#TITULO DE LA VENTANA
win.title("Lectura sensores")

#DIMENSIONES DE INICIO
win.geometry('1050x500')

#FUENTES
myFont = tkFont.Font(family = 'Helvetica', size = 36)
titles = tkFont.Font(family = 'Helvetica', size = 50, weight = 'bold')

#FUNCIONES
##LEER SENSORES
def med():
    ###DISTANCIA
    global distance
    global distancia
    global temp
    global humd
    GPIO.output(trig, False)
    time.sleep(1)
    GPIO.output(trig, True)
    time.sleep(0.00001)
    GPIO.output(trig, False)

    while GPIO.input(echo)==0:
        pulse_start = time.time()

    while GPIO.input(echo)==1:
        pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start

    distance = pulse_duration * 17150
    distance = round(distance, 2)

    distancia.configure(text=str(distance))
    distanciaM.append(str(distance))
    print(distanciaM)
    ###PRESENCIA
    i=GPIO.input(pir)
    if i==1:
        rb1.configure(text = str(i))
        time.sleep(0.1)
    else:
        rb1.configure(text = str(i))
        time.sleep(0.1)
    presenciaM.append(str(i))
    print (presenciaM)
    ###TEMPERATURA Y HUMEDAD
    humidity, temperature = Adafruit_DHT.read_retry(11, 27)
    temp = temperature
    humd = humidity
    templl.configure(text=str(temp))
    humll.configure(text=str(humd))
    temperaturaM.append(str(temp))
    humedadM.append(str(humd))
    print(temperaturaM)
    print(humedadM)
    ###GRAFICAR
    plt.plot(distanciaM, label ='Distancia','r')
    plt.plot(presenciaM, label='Presencia', 'g')
    plt.plot(temperaturaM, label='Temperatura', 'b')
    plt.plot(humedadM, label='Humedad', 'y')
    plt.show()
##LECTURA CONTINUA
def leccont():
    med()
    win.after(1000, leccont)
##SALIR DEL PROGRAMA
def exitProgram():
    GPIO.cleanup()
    win.quit()

#WIDGETS

##TITULO
titulo  = Label(win, text="Lectura de cuatro sensores", font = titles)
titulo.grid(columnspan = 3)

##DISTANCIA
dist = Label(win, text = "Distancia:", font = myFont)
dist.grid(row = 1, column=0, sticky=W)

##ETIQUETA DISTANCIA
distancia = Label(win, text = "Distancia" , font=myFont)
distancia.grid(row=1, column=1)

##ETIQUETA UNIDADES DISTANCIA
cms=Label(win, text = "cms", font = myFont)
cms.grid(row = 1, column = 2)

##ETIQUETA PRESENCIA
pa=Label(win, text = "Presencia:", font = myFont)
pa.grid(row=2, column = 0, sticky=W)

##INDICADOR PRESENCIA
rb1 = Label(win, text = "No Presente", font = myFont)
rb1.grid(row = 2, column =1)

##TEMPERATURA
templ=Label(win, text="Temperatura:", font=myFont)
templ.grid(row=3,column=0,sticky=W)

##ETIQUETA TEMPERATURA
templl=Label(win, text="Temperatura", font=myFont)
templl.grid(row=3,column=1)

##ETIQUETA UNIDADES TEMPERATURA
tempu=Label(win, text = "C", font = myFont)
tempu.grid(row = 3, column = 2)

##HUMEDAD
huml=Label(win, text="Humedad relativa:", font=myFont)
huml.grid(row=4,column=0,sticky=W)

##ETIQUETA HUMEDAD
humll=Label(win, text="humedad", font=myFont)
humll.grid(row=4,column=1)

##ETIQUETA UNIDADES HUMEDAD
humu=Label(win, text = "HR", font = myFont)
humu.grid(row = 4, column = 2)

##MOSTRAR VALORES
medir=Button(win,text="Medir",font=myFont,command=med,height=1,width=6)
medir.grid(row=6,column=0)

##LECTURA CONTINUA
lecconti=Button(win, text="Medicion continua", font = myFont, command = leccont, height = 1, width = 15)
lecconti.grid(row = 6, column = 1)

##BOTON SALIR
exitButton  = Button(win, text = "Salir", font = myFont, command = exitProgram, height = 1 , width = 6) 
exitButton.grid(row = 6, column = 2)

mainloop()

我已经解决了,所以这是代码:

# -*- coding: utf-8 -*-
#IMPORTAR LIBRERIAS NECESARIAS
from Tkinter import *
from drawnow import *
from matplotlib import style
import RPi.GPIO as GPIO
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy
import tkFont
import time
import Adafruit_DHT

#Variables
trig = 16
echo = 18
ldr = 15
distance = 0
distancia = 0
tiempo = 0
temp = 0
humd = 0
distanciaM = []
temperaturaM = []
resistenciaM = []
humedadM = []

style.use('fivethirtyeight')

#DECLARAR ENTRADAS Y SALIDAS
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(trig, GPIO.OUT)
GPIO.setup(echo, GPIO.IN)
GPIO.setup(ldr, GPIO.OUT)


#VENTANAS
win = Tk()
fig = plt.figure()

#TITULO DE LA VENTANA
win.title("Lectura sensores")

#DIMENSIONES DE INICIO
win.geometry('900x500')

#FUENTES
myFont = tkFont.Font(family = 'Helvetica', size = 36)
titles = tkFont.Font(family = 'Helvetica', size = 50, weight = 'bold')

#FUNCIONES
##LEER SENSORES
def med(i):
    ###DISTANCIA
    global distance
    global distancia
    global temp
    global humd
    GPIO.output(trig, False)
    time.sleep(1)
    GPIO.output(trig, True)
    time.sleep(0.00001)
    GPIO.output(trig, False)

    while GPIO.input(echo)==0:
        pulse_start = time.time()

    while GPIO.input(echo)==1:
        pulse_end = time.time()

    pulse_duration = pulse_end - pulse_start

    distance = pulse_duration * 17150
    distance = round(distance, 2)

    distancia.configure(text=str(distance))
    distanciaM.append(str(distance))
    print(distanciaM)
    ###RESISTENCIA
    count = 0
    GPIO.setup(ldr, GPIO.OUT)
    GPIO.output(ldr, GPIO.LOW)
    time.sleep(0.1)
    GPIO.setup(ldr, GPIO.IN)
    while (GPIO.input(ldr) == GPIO.LOW):
        count += 1
        rb1.configure(text = str(count))
    print count
    resistenciaM.append(str(count))
    print (resistenciaM)
    ###TEMPERATURA Y HUMEDAD
    humidity, temperature = Adafruit_DHT.read_retry(11, 27)
    temp = temperature
    humd = humidity
    templl.configure(text=str(temp))
    humll.configure(text=str(humd))
    temperaturaM.append(str(temp))
    humedadM.append(str(humd))
    print(temperaturaM)
    print(humedadM)
    ###GRAFICAR
    plt.plot(distanciaM, label ='Distancia',color='r')
    plt.plot(resistenciaM, label='Resistencia', color='g')
    plt.plot(temperaturaM, label='Temperatura', color='b')
    plt.plot(humedadM, label='Humedad', color='k')
    count = 0
##LECTURA CONTINUA
def leccont():
    ani = animation.FuncAnimation(fig, med, interval=2000)
    plt.legend()
    plt.show()
    win.after(1000, leccont)
##SALIR DEL PROGRAMA
def exitProgram():
    GPIO.cleanup()
    plt.close()
    win.quit()
#WIDGETS

##TITULO
titulo  = Label(win, text="Lectura de cuatro sensores", font = titles)
titulo.grid(columnspan = 3)

##DISTANCIA
dist = Label(win, text = "Distancia:", font = myFont)
dist.grid(row = 1, column=0, sticky=W)

##ETIQUETA DISTANCIA
distancia = Label(win, text = "Distancia" , font=myFont)
distancia.grid(row=1, column=1)

##ETIQUETA UNIDADES DISTANCIA
cms=Label(win, text = "Cms", font = myFont)
cms.grid(row = 1, column = 2)

##ETIQUETA RESISTENCIA
pa=Label(win, text = "Resistencia:", font = myFont)
pa.grid(row=2, column = 0, sticky=W)

##INDICADOR RESISTENCIA
rb1 = Label(win, text = "Resistencia", font = myFont)
rb1.grid(row = 2, column =1)

##ETIQUETA RESITENCIA
ohms=Label(win, text="Ohms", font = myFont)
ohms.grid(row=2, column = 2)

##TEMPERATURA
templ=Label(win, text="Temperatura:", font=myFont)
templ.grid(row=3,column=0,sticky=W)

##ETIQUETA TEMPERATURA
templl=Label(win, text="Temperatura", font=myFont)
templl.grid(row=3,column=1)

##ETIQUETA UNIDADES TEMPERATURA
tempu=Label(win, text = "C", font = myFont)
tempu.grid(row = 3, column = 2)

##HUMEDAD
huml=Label(win, text="Humedad relativa:", font=myFont)
huml.grid(row=4,column=0,sticky=W)

##ETIQUETA HUMEDAD
humll=Label(win, text="humedad", font=myFont)
humll.grid(row=4,column=1)

##ETIQUETA UNIDADES HUMEDAD
humu=Label(win, text = "HR", font = myFont)
humu.grid(row = 4, column = 2)

##LECTURA CONTINUA
lecconti=Button(win, text="Medicion", font = myFont, command = leccont, height = 1, width = 15)
lecconti.grid(row = 6, column = 0)

##BOTON SALIR
exitButton  = Button(win, text = "Salir", font = myFont, command = exitProgram, height = 1 , width = 6) 
exitButton.grid(row = 6, column = 2)

mainloop()