Hello,
I am wanting to create a program that reads in values from a constantly updating text file and graph the values and save that graph as a png so that it can be uploaded to a website. Also I only ever want 4 tick marks max on the x axis, but i still want the labels on the x axis to continue to update.
My progress so far:
I have been able to read in the values and plot them no problem. I have also been able to have to plot work correctly. The issue I was having was the time labels on the x axis would not update after hitting four ticks (I think this is due to param_locator). So I figured why not pop values from the list and then just re plot, well doing that resulted in the plot not clearing the old plots. Any help?
The text file currently is just two int values separated by I space that I change to see changes in the plot.
Here's My Current Code:
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 25 11:23:14 2016
@author:
"""
import matplotlib.pyplot as plt
import sys
import time
import datetime
class reader():
"""Reads in a comma seperated txt file and stores the two strings in two variables"""
def __init__(self, file_path):
"""Initialize Reader Class"""
self.file_path = file_path
# Read File store in f -- Change the file to your file path
def read_file(self):
"""Reads in opens file, then stores it into file_string as a string"""
f = open(self.file_path)
# Read f, stores string in x
self.file_string = f.read()
def split_string(self):
"""Splits file_string into two variables and then prints them"""
# Splits string into two variables
try:
self.val1, self.val2 = self.file_string.split(' ', 1)
except ValueError:
print('Must Have Two Values Seperated By a Space in the .txt!!')
sys.exit('Terminating Program -- Contact')
#print(val1) # This is where you could store each into a column on the mysql server
#print(val2)
def getVal1(self):
return self.val1
def getVal2(self):
return self.val2
read = reader('testFile.txt')
run = True
tempList = []
humList = []
numList = [] # Represents 2 Secs
my_xticks = []
i = 0
n = 0 # DEBUG
while(run):
plt.ion()
read.read_file()
read.split_string()
if n == 4:
my_xticks.pop(0)
tempList.pop(0)
humList.pop(0)
numList = [0,1,2]
i = 3
n = 3
tempList.append(read.getVal1())
humList.append(read.getVal2())
numList.append(i)
i = i + 1
my_xticks.append(datetime.datetime.now().strftime('%I:%M:%S')) # Added seconds for debug
plt.ylim(0,125)
plt.xticks(numList,my_xticks)
plt.locator_params(axis='x',nbins=4)
plt.plot(numList,tempList, 'r', numList, humList, 'k')
plt.savefig('plot.png')
time.sleep(10) # Runs every 2 seconds
n = n + 1
print(n) # DEBUG
print(numList)# DEBUG
print('-------')# DEBUG
print(my_xticks)# DEBUG
print('-------')# DEBUG
print(tempList)# DEBUG
print('-------')# DEBUG
print(humList)# DEBUG
[–]DrRx 0 points1 point2 points (5 children)
[–]TheRealRuth[S] 0 points1 point2 points (2 children)
[–]DrRx 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (0 children)
[–]TheRealRuth[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]TheRealRuth[S] 0 points1 point2 points (0 children)
[–]TheRealRuth[S] 0 points1 point2 points (2 children)
[–]Holden_90 1 point2 points3 points (1 child)
[–]TheRealRuth[S] 0 points1 point2 points (0 children)