我正在從一個項目中讀取一個文本文件中的值,該文本文件通過由空格分隔的兩個值進行動態更新。將這些值放入列表中,然後將它們繪製爲每個都是y軸上的一個點,並且時間是x軸。在下面提供的第一組代碼中,我可以獲取這些值並繪製它們,然後將該圖保存爲png。然而,隨着更多數據的進入,圖表似乎沒有更新時間值。但圖表反映了值的變化。基於動態值更新Matplotlib中的軸
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 25 11:23:14 2016
@author: aruth3
"""
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 Austin')
#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
while(run):
plt.ion()
read.read_file()
read.split_string()
tempList.append(read.getVal1())
humList.append(read.getVal2())
numList.append(i)
i = i + 1
my_xticks.append(datetime.datetime.now().strftime('%I:%M'))
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
的TESTFILE.TXT有兩個值100 90
,可以在圖中的動態和變化進行更新。但隨着時間的推移,您會注意到(如果您運行代碼)時間不會更新。
爲了彌補沒有更新的時間問題我認爲使用pop修改列表將允許第一個值離開,然後再循環時返回另一個值。這隻要時間更新了有關的工作,然而這最終搞亂圖: Link To Bad Graph Image
代碼:
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 2 09:42:16 2016
@author: aruth3
"""
# -*- 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
所以我的問題是如何創建一個圖表,當新的價值觀來在其中列出了列表中的第一個值,從而更新了時間,同時還提供了數據的準確圖形而不會出現毛刺?
從列表中彈出似乎是一個好主意,但我不知道爲什麼它搞亂了圖形?
謝謝!