Python Forum
Axis lim and Plotting a graph in Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Axis lim and Plotting a graph in Tkinter
#1
Hi everybody! I'm designing a test bank than shows me 3 temperatures of a vacuum pump, the current of the motor than impulse that pump, the rpm of the vacuum pump, the KPa of the pump and i want to save all the data reading by the sensors in the test time in a txt archive at the same time I want to see all those values in a graphic interface in Python 3.7 and here start the headaches Big Grin

So i began with the 3 temperature values, for this i connected 3 termocuples to an Arduino UNO board and i made this code:

This is of course 'C' code, still looks better in 'Python' frame:
//This code sense 3 temperatures detected by Type "K" thermocuples

#include <max6675.h> //Librería del MAX6675

unsigned long interval=5000; //
unsigned long previousMillis=0; // 

int ktcSO = 8;
int ktcCS1 = 9; // Thermocuple 1
int ktcCLK = 10;
int ktcCS2 = 11; // Thermocuple 2
int ktcCS3 = 6; // Thermocuple 3

int temp1; //
int temp2; // 
int temp3; //
int contadordetiempo = 0; 

MAX6675 ktc1(ktcCLK, ktcCS1, ktcSO);
MAX6675 ktc2(ktcCLK, ktcCS2, ktcSO);
MAX6675 ktc3(ktcCLK, ktcCS3, ktcSO);

void setup() 
  {
  Serial.begin(9600); /
  }

void loop() 

  {
  unsigned long currentMillis = millis(); 
  if ((unsigned long)(currentMillis - previousMillis) >= interval)
    {
    contadordetiempo = contadordetiempo + 5; 
    Serial.print(contadordetiempo); //Esto lo imprime python


    Serial.print("\t"); 
    temp1 = ktc1.readCelsius();
    Serial.print(temp1)
  
    Serial.print("\t"); 

    temp2 = ktc2.readCelsius();
    Serial.print(temp2);
    
    Serial.print("\t");

    
    temp3 = ktc3.readCelsius();
    Serial.print(temp3);
   
  
    Serial.print("\n");  
    
    previousMillis = millis(); 
    }
  }
Then i design this code to watch my values in a graphic

import serial
import time 
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from tkinter import *
from matplotlib.axes import *

arduino= serial.Serial("COM6",9600)

class system:

	def __init__(self,master):

		self.master=master
		self.fig = plt.figure(figsize=(8,8), dpi=80)
		self.ax1 = self.fig.add_subplot(111)
		plt.ion()
		self.t1=[]
		self.t2=[]
		self.t3=[]
		self.tiempo=[]
		self.botonalm=Button(master, text="ALMACENAR", command=self.guardardata,font=("Arial Black",14),bg="cyan").place(x=200,y=450)
		self.botonsalir=Button(master, text="SALIR",command=master.quit,font=("Arial Black",14),bg="red",fg="white").place(x=450,y=450)
		self.botoncontinuar=Button(master, text="EMPEZAR",command=self.animacion,font=("Arial Black",14),bg="green",fg="white").place(x=650,y=450)
		
	def guardardata(self):
		
		archivo=open("data.txt","a")
		archivo.write("\nData Banco de prueba de bombas de vacío\n")
		archivo.write("Fecha: "+str(time.localtime()[2])+"-"+str(time.localtime()[1])+"-"+str(time.localtime()[0])+"\nHora: "+str(time.localtime()[3])+":"+str(time.localtime()[4])+"\n")
		archivo.write("MARCA: "+ marca.get() + "\n" + "MODELO: " + modelo.get() + "\n" +"RODAMIENTOS: "+ rodamiento.get() +"\n"+ "ESTOPERA: "+ estopera.get() +"\n"+"PALETAS: "+ paleta.get() +"\n"+"CARACTERÍSTICAS GENERALES: "+ caracGen.get() + "\n" + "OBSERVACIONES: " + obs.get() + "\n")

	def animada(self,i):
		
		
		while i<=200:	

			i += 1
			e = arduino.readline().decode("utf-8").strip("\n")
			s=e.split("\t")
			self.contador= s[0]
			self.temp1= s[1]
			self.temp2= s[2]
			self.temp3= s[3]

			self.t1.append(self.temp1)
			self.t2.append(self.temp2)
			self.t3.append(self.temp3)
			self.tiempo.append(self.contador)


			readingcount.set(self.contador)
			readingtemp1.set(self.temp1)
			readingtemp2.set(self.temp2)
			readingtemp3.set(self.temp3)

			archivolectura.write(str(self.contador) +"\t"+ str(self.temp1) +"\t"+ str(self.temp2) +"\t"+ str(self.temp3) +"\n")
			
			#Para ver detalles en la shell 
			print("Temp 1 = " + self.temp1)
			print("Temp 2 = " + self.temp2)
			print("Temp 3 = " + self.temp3)
			print("Tiempo de prueba = " + self.contador)


			self.line1=plt.plot(self.tiempo, self.t1)
			self.line2=plt.plot(self.tiempo, self.t2)
			self.line3=plt.plot(self.tiempo, self.t3)

			plt.setp(self.line1, color='blue')
			plt.setp(self.line2, color='orange')
			plt.setp(self.line3, color='green')

			return self.line1, self.line2, self.line3


	def animacion(self):
		
		self.ani=animation.FuncAnimation(self.fig, self.animada)
		ejecutar(mostrar(fase2))
		plt.show()
	
archivolectura=open("lecturas.txt","a")
archivolectura.write("\nBanco de pruebas para bombas de vacío - Industrias BJT\n")
archivolectura.write("Fecha: "+str(time.localtime()[2])+"-"+str(time.localtime()[1])+"-"+str(time.localtime()[0])+"\nHora: "+str(time.localtime()[3])+":"+str(time.localtime()[4])+"\n")
archivolectura.write("\nt\tT1\tT2\tT3\n")	

fase1= Tk()
fase1.title("FASE 1 : CARACTERISTICAS DE LA BOMBA")
fase1.geometry("1020x600")
my_gui = system(fase1)
fase2 = Toplevel(fase1)
fase2.title("FASE 2: MEDICIÓN DE VARIABLES")
fase2.geometry("1020x600")
fase2.config(bg="black")

def mostrar(ventana): 
	ventana.deiconify()
def ocultar(ventana):
	ventana.withdraw()
def ejecutar(f): 
		fase1.after(200,f)

ocultar(fase2)
marca=StringVar()
modelo=StringVar()
rodamiento=StringVar()
estopera=StringVar()
paleta=StringVar()
caracGen=StringVar()
obs=StringVar()

titulo=Label(fase1,text="BANCO DE PRUEBAS PARA BOMBAS DE VACÍO",font=("Arial Black", 18),fg="Black").place(x=200, y=10)
indi=Label(fase1,text="INTRODUZCA LAS CARACTERISTICAS DE LA BOMBA DE VACÍO A PROBAR",font=("Arial", 14),fg="red").place(x=200, y=60)

ema=Label(fase1,text="MARCA: ",font=("Arial Black",14)).place(x=50, y=120)
emaCaja=Entry(fase1,textvariable=marca,font=("Arial Black",14)).place(x=150, y=120) 
emo=Label(fase1,text="MODELO: ",font=("Arial Black",14)).place(x=50, y=200)	
emoCaja=Entry(fase1,textvariable=modelo,font=("Arial Black",14)).place(x=160, y=200)
rod=Label(fase1,text="RODAMIENTOS: ",font=("Arial Black",14)).place(x=500, y=90)
rodCaja=Entry(fase1,textvariable=rodamiento,font=("Arial Black",14)).place(x=680, y=90)
est=Label(fase1,text="ESTOPERAS: ",font=("Arial Black",14)).place(x=500, y=170)
estCaja=Entry(fase1,textvariable=estopera,font=("Arial Black",14)).place(x=680, y=170)
pal=Label(fase1,text="PALETAS: ",font=("Arial Black",14)).place(x=500, y=250)
palCaja=Entry(fase1,textvariable=paleta,font=("Arial Black",14)).place(x=680, y=250)
cG=Label(fase1,text="CARACTERÍSTICAS\nGENERALES: ",font=("Arial Black",14)).place(x=300, y=300)	
cGCaja=Entry(fase1,textvariable=caracGen,font=("Arial Black",14)).place(x=550, y=300)
eobs=Label(fase1,text="OBSERVACIONES: ",font=("Arial Black",14)).place(x=300, y=400)	
obsCaja=Entry(fase1,textvariable=obs,font=("Arial Black",14)).place(x=550, y=400)

Titulo = Label(fase2, text= 'Banco de pruebas de bombas de vacío', bg="black",fg = 'yellow', font = ("Arial Black", 20)).place(x=10,y=0)
bjt=Label(fase2,text= "Industrias BJT C.A.", bg="black",fg = 'white', font = ("Arial", 12)).place(x=10,y=40)
#btn1= Button(ventana,text= "SALIDA \n DE \n EMERGENCIA", command=cerrar, fg= "white", bg= "red",relief=SOLID).place(x=800,y=100)
#ESTAMIERDANOMECIERRAJODER

#Tipo de datos entregados.
readingcount=StringVar()
readingtemp1=StringVar()
readingtemp2=StringVar()
readingtemp3=StringVar()
#readingcurr=StringVar()
#readingpres=StringVar()
#readingrpm=StringVar()

imagen1=PhotoImage(file="valid.png")
parametros=Label(fase2, image=imagen1).place(x=730,y=10)

#Variables Físicas
temperaturas=Label(fase2, text= "TEMPERATURA (ºC)", bg="black", font=("Arial Black",14),fg="white").place(x=10,y=70)
temperatura1Caja=Label(fase2, text="T1=", bg="blue",font=("Lucida Console", 14),fg="white").place(x=20, y=100)
temperatura1=Label(fase2,textvariable = readingtemp1,bg="blue",font=("Lucida Console", 14), fg="white").place(x=50,y=100)
temperatura2Caja=Label(fase2, text="T2=", bg="orange",font=("Lucida Console", 14),fg="white").place(x=150, y=100)
temperatura2=Label(fase2,textvariable = readingtemp2,bg="orange",font=("Lucida Console", 14), fg="white").place(x=180,y=100)
temperatura3Caja=Label(fase2, text="T3=", bg="green",font=("Lucida Console", 14),fg="white").place(x=280, y=100)
temperatura3=Label(fase2,textvariable = readingtemp3,bg="green",font=("Lucida Console", 14), fg="white").place(x=310,y=100)
rpmCaja=Label(fase2, text="RPM=", bg= "light green", font=("Lucida Console",14), fg="black").place(x=20,y=500)
rpm=Label(fase2, text="1200", bg="light green", font=("Lucida Console",14), fg="black").place(x=70,y=500)
presionCaja=Label(fase2,text="PRESION (-KPa)=", bg="light blue", font=("Lucida Console",14), fg="black").place(x=150,y=500)
presion=Label(fase2,text="50", bg="light blue",font=("Lucida Console",14), fg="black").place(x=320,y=500)
corrienteCaja=Label(fase2, text= "CORRIENTE DEL MOTOR (A)=", bg="light yellow", font=("Lucida Console",14), fg="black").place(x=380, y=500)
corriente=Label(fase2, text="8.80" ,bg="light yellow",font=("Lucida Console",14),fg="black").place(x=650, y=500)

#Contador de tiempo :v 

TiempoCaja=Label(fase2, text= "TIEMPO DE PRUEBA (seg.)=", bg="white", font=("Lucida Console",14), fg="black").place(x=400, y=100)
Tiempo=Label(fase2,textvariable=readingcount ,text=" seg.",bg="white",font=("Lucida Console",14),fg="black").place(x=670, y=100)

marfase2Caja=Label(fase2,text = "MARCA: ",bg="black",font=("Lucida Console", 14), fg="white").place(x=730,y=200)
marfase2=Label(fase2,textvariable = marca,bg="black",font=("Lucida Console", 14), fg="white").place(x=800,y=200)
modfase2Caja=Label(fase2,text = "MODELO: ",bg="black",font=("Lucida Console", 14), fg="white").place(x=730,y=230)
modfase2=Label(fase2,textvariable = modelo,bg="black",font=("Lucida Console", 14), fg="white").place(x=810,y=230)
rodfase2Caja=Label(fase2,text = "RODAMIENTOS: ",bg="black",font=("Lucida Console", 14), fg="white").place(x=730,y=260)
rodfase2=Label(fase2,textvariable = rodamiento,bg="black",font=("Lucida Console", 14), fg="white").place(x=870,y=260)
estfase2Caja=Label(fase2,text = "ESTOPERAS: ",bg="black",font=("Lucida Console", 14), fg="white").place(x=730,y=290)
estfase2=Label(fase2,textvariable = estopera,bg="black",font=("Lucida Console", 14), fg="white").place(x=850,y=290)
palfase2Caja=Label(fase2,text = "PALETAS: ",bg="black",font=("Lucida Console", 14), fg="white").place(x=730,y=320)
palfase2=Label(fase2,textvariable = paleta,bg="black",font=("Lucida Console", 14), fg="white").place(x=830,y=320)
cGfase2Caja=Label(fase2,text = "CARACTERISTICAS: ",bg="black",font=("Lucida Console", 14), fg="white").place(x=730,y=350)
cGfase2=Label(fase2,textvariable = caracGen,bg="black",font=("Lucida Console", 14), fg="white").place(x=910,y=350)
obsfase2Caja=Label(fase2,text = "OBSERVACIONES: ",bg="black",font=("Lucida Console", 14), fg="white").place(x=730,y=400)
obsfase2=Label(fase2,textvariable = obs,bg="black",font=("Lucida Console", 14), fg="white").place(x=900,y=400)


fase1.mainloop()
and when i run it shows me this İmage

and when i push the bottom "EMPEZAR" shows me this
İmage


And i want to plot my graph in my interface Cry by the way i can't put the axes limits in my graph (and that results into a complete disaster as seen in the second image)

i don't know how to do this i'm a newbie in Python Wall Doh if anybody coulds help me i'll be very grateful.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Tkinter Matplotlib Animation Graph not rendering dimidgen 3 335 Mar-12-2024, 02:09 PM
Last Post: deanhystad
  [Tkinter] Plotting Raster Data / TIFF files on tkinter canvas RRSCNGP 0 2,536 Aug-28-2019, 04:44 AM
Last Post: RRSCNGP
  [Tkinter] how to remove black area around the graph in tkinter ? NEL 1 2,243 Aug-03-2019, 01:48 PM
Last Post: NEL
  Tkinter - Make changes to graph and update it adriancovaci 0 6,531 Apr-08-2019, 09:02 AM
Last Post: adriancovaci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020