我只是將腳趾浸入Python中,並且在這裏獲得了一個實時更新Matplotlib圖的幫助。該方案採用動畫從一個動態增長的CSV文件中提取數據,看起來像下面的數據:Python數據顯示圖
TimeStamp, ReadCount, Antenna, Protocol, RSSI, EPC, Sensor
09/28/2016 17:34:28.967, 5686, 2, GEN2, -25, E036115348A908CB, 23.16,0.00,0.00, (Infinity%),
09/28/2016 17:34:29.716, 5687, 2, GEN2, -32, E036115348A908CB, (Infinity%),
09/28/2016 17:34:31.155, 5689, 2, GEN2, -27, E036115348A908CB, 22.74,3.38, (Infinity%),
09/28/2016 17:34:32.351, 5692, 2, GEN2, -25, E036115348A908CB, 22.95,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:32.895, 5695, 2, GEN2, -23, E036115348A908CB, 22.95,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:33.397, 5698, 2, GEN2, -21, E036115348A908CB, 23.78,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:33.946, 5699, 2, GEN2, -23, E036115348A908CB, 23.57,0.00,3.38, (Infinity%),
09/28/2016 17:34:34.912, 5702, 2, GEN2, -27, E036115348A908CB, 23.36,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:35.394, 5705, 2, GEN2, -25, E036115348A908CB, 23.36,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:35.988, 5707, 2, GEN2, -23, E036115348A908CB, 23.78,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:36.489, 5710, 2, GEN2, -21, E036115348A908CB, 23.99,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:37.269, 5712, 2, GEN2, -23, E036115348A908CB, 23.78,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:37.796, 5715, 2, GEN2, -18, E036115348A908CB, 23.78,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:38.296, 5718, 2, GEN2, -7, E036115348A908CB, 22.32,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:38.826, 5721, 2, GEN2, -7, E036115348A908CB, 23.57,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:39.320, 5724, 2, GEN2, -30, E036115348A908CB, 23.36,0.00,0.00,3.38, (Infinity%),
09/28/2016 17:34:39.870, 5727, 2, GEN2, -9, E036115348A908CB, 23.36,0.00,0.00,3.38, (Infinity%),
這個數據是從傳感器進來,我想能夠拉/顯示一些此數據與此圖形一起顯示在其他值中。對於情節的代碼如下所示:
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from datetime import datetime, timedelta
import collections
import csv
offset = 16
slope = -.2081
def plot(ax, data, colour, width):
if data:
last_dt = data[0][0]
gap = timedelta(seconds=10)
x = []
y = []
# Plot groups of data not more than 60 seconds apart
for dt, ten in data:
if dt <= last_dt + gap:
x.append(dt)
y.append(ten)
else:
ax.plot(matplotlib.dates.date2num(x), y, colour, linewidth=width)
x = [dt]
y = [ten]
last_dt = dt
ax.plot(matplotlib.dates.date2num(x), y, colour, linewidth=width)
def animate(i, fig, ax):
# Read in the CSV file
data = collections.defaultdict(list)
fields = ["TimeStamp", "ReadCount", "Antenna", "Protocol", "RSSI", "EPC", "Temp", "Ten", "Powr", "Unpowr", "Inf"]
with open('SensorLogFile.csv') as f_input:
csv_input = csv.DictReader(f_input, skipinitialspace=True, fieldnames=fields)
header = next(csv_input)
# Separate the rows based on the Antenna field
for row in csv_input:
try:
data[row['Antenna']].append(
[datetime.strptime(row['TimeStamp'], '%m/%d/%Y %H:%M:%S.%f'),
int(float(row['Ten']) * float(slope) + float(offset))])
except:
pass
# Drop any data points more than 1.5 mins older than the last entry
latest_dt = data[row['Antenna']][-1][0] # Last entry
not_before = latest_dt - timedelta(minutes=.25)
for antenna, entries in data.items():
data[antenna] = [[dt, count] for dt, count in entries if dt >= not_before]
# Redraw existing axis
ax.clear()
ax.spines['bottom'].set_color("#5998ff")
ax.spines['top'].set_color("#5998ff")
ax.spines['left'].set_color("#5998ff")
ax.spines['right'].set_color("#5998ff")
hfmt = matplotlib.dates.DateFormatter('%m/%d/%Y\n%I:%M:%S %p')
ax.xaxis.set_major_formatter(hfmt)
fig.autofmt_xdate()
plot(ax, data['1'], 'c', 6) # Antenna 1
plot(ax, data['2'], 'r', 6) # Antenna 2
plot(ax, data['3'], 'y', 6) # Antenna 3
ax.grid(True, color='w')
plt.ylabel('Tension (lb)', color='w', fontsize=20)
plt.title('Spiral 1 Tension', color='w', fontsize=26)
ax.tick_params(axis='y', colors='w')
ax.tick_params(axis='x', colors='w')
# ax.set_ylim([21,28])
fig = plt.figure(facecolor='#07000d')
ax = fig.add_subplot(111, axisbg='#07000d')
ani = animation.FuncAnimation(fig, animate, fargs=(fig, ax), interval=250)
plt.show()
我希望能有在上面的圖表和圖形下方的空白區域(圖中的全寬),在那裏我可以把以下(可能有一個框,每一個這些數據的):
- 「TEMP」(第7每行CSV,只需要轉換爲華氏度)
- 「時代」(當前時間)
- 「Overdrive」(未顯示任何與此數據有關的信息,但僅爲將來的佔位符)
- 「循環」(將是天線1的微小間隙之後先讀的次數)
- 盒與右下方的圖像 - 爲對例如可以說這個圖像
我已經研究過使用Tkinter並試圖遵循sentdex的video here
但一直沒有能夠得到它的工作。
我處於時間緊縮狀態,無法再單獨對此進行鬥爭 - 需要專業人士的幫助。任何人都可以給我一些方向/指導如何使顯示像我在找什麼?
1)理想的情況下,是的,實時的將是很好。我現在每隔250毫秒就會運行一次,這與現實時間非常接近。 2)當數據被寫入csv(並非全部時間)時,每250ms會有新值出現。 3)不需要任何交互性,只需要顯示頂部的圖形和底部的單元格以顯示我提到的實時數據並放置照片。哦,所以我只用Matplotlib就可以做到這一切?劇情創作代碼看起來像拉取數據(即溫度),通過公式運行並在單元格中顯示實時輸出數字? – Jeffrey
所以你想要數字格式的「Temp」「Time」「Overdrive」「Cycles」,而不是圖形? Matplotlib當然可以顯示簡單的文本,使用'ax.text(x_coord,y_coord,「E036115348A908CB」)'繪圖都不是問題。問題是每250ms讀取一次文件。 – ImportanceOfBeingErnest
這與我正在尋找的東西非常接近!我已經測試過了,它看起來像X軸沒有像以前那樣顯示最後的'.25'分鐘的數據,現在它似乎被卡住了,實際上並沒有顯示新的數據。溫度效果很好,但是對於時間顯示,我不想從時間數據中提取時間,只是使用計算機時鐘或其他東西(否則當傳感器超出範圍時,時間會凍結)。 – Jeffrey