繁体   English   中英

如何通过从 excel 文件中获取数据并在 5 秒后使用 python 在图形中绘制数据来显示实时图形

[英]How do I display real-time graphs from fetching data from excel file and plotting the data after 5 sec in graph using python

这是我的代码,我必须从 excel 文件中获取数据,它正在获取两列,并且我还使用了“gridsepec”来在不同的轴上显示每个图形,但我希望每个数据应该在每 5 秒后绘制一次,直到excel表格的最后一行,并实时使用python,如果可能,添加一个按钮以根据它在图表中绘制的值在按钮中显示数据值:

from openpyxl import load_workbook
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

# read  from excel file
wb = load_workbook('C:\\Users\\Acer-i5-607\\Desktop\\case1.xlsx')
sheet_1 = wb.get_sheet_by_name('case1')

plt.figure(figsize=(6, 4), facecolor='Grey')
G = gridspec.GridSpec(6, 2)
axes_1 = plt.subplot(G[0, :])


x = np.zeros(sheet_1.max_row)
y = np.zeros(len(x))

for i in range(1, sheet_1.max_row):
    x[i] = sheet_1.cell(row=i + 1, column=2).value
    y[i] = sheet_1.cell(row=i + 1, column=4).value

#print x
#print y

# create the plot
plt.xlabel('time')
plt.ylabel('HR')
plt.plot(x, y, color='cyan', label='HR')
plt.legend(loc='upper right', fontsize='small')
plt.grid(True)
#plt.title('Reading values from an Excel file'

axes_1 = plt.subplot(G[1, :])

x = np.zeros(sheet_1.max_row)
y = np.zeros(len(x))

for i in range(1, sheet_1.max_row):
    x[i] = sheet_1.cell(row=i + 1, column=2).value
    y[i] = sheet_1.cell(row=i + 1, column=6).value

#print a
#print b

# create the plot
plt.xlabel('time')
plt.ylabel('Pulse')
plt.plot(x, y, color='red', label='Pulse')
plt.legend(loc='upper right', fontsize='small')
plt.grid(True)


axes_1 = plt.subplot(G[2, :])

x = np.zeros(sheet_1.max_row)
y = np.zeros(len(x))

for i in range(1, sheet_1.max_row):
    x[i] = sheet_1.cell(row=i + 1, column=2).value
    y[i] = sheet_1.cell(row=i + 1, column=7).value

#print x
#print y

# create the plot
plt.xlabel('time')
plt.ylabel('SpO2')
plt.plot(x, y, color='magenta', label='SpO2')
plt.legend(loc='upper right', fontsize='small')
plt.grid(True)


axes_1 = plt.subplot(G[3, :])

x = np.zeros(sheet_1.max_row)
y = np.zeros(len(x))

for i in range(1, sheet_1.max_row):
    x[i] = sheet_1.cell(row=i + 1, column=2).value
    y[i] = sheet_1.cell(row=i + 1, column=8).value

#print x
#print y

# create the plot
plt.xlabel('time')
plt.ylabel('Perf')
plt.plot(x, y, color='blue', label='Perf')
plt.legend(loc='upper right', fontsize='small')
plt.grid(True)


axes_1 = plt.subplot(G[4, :])

x = np.zeros(sheet_1.max_row)
y = np.zeros(len(x))

for i in range(1, sheet_1.max_row):
    x[i] = sheet_1.cell(row=i + 1, column=2).value
    y[i] = sheet_1.cell(row=i + 1, column=9).value

#print x
#print y

# create the plot
plt.xlabel('time')
plt.ylabel('etCO2')
plt.plot(x, y, color='yellow', label='etCO2')
plt.legend(loc='upper right', fontsize='small')
plt.grid(True)


axes_1 = plt.subplot(G[5, :])

x = np.zeros(sheet_1.max_row)
y = np.zeros(len(x))

for i in range(1, sheet_1.max_row):
    x[i] = sheet_1.cell(row=i + 1, column=2).value
    y[i] = sheet_1.cell(row=i + 1, column=10).value

#print x
#print y

# create the plot
plt.xlabel('time')
plt.ylabel('imCO2')
plt.plot(x, y, color='green', label='imCO2')
plt.legend(loc='upper right', fontsize='small')
plt.grid(True)

plt.xlim(0, 60000)
plt.ylim(0, 100)
plt.show()

经过多次尝试,我终于找到了答案,希望它也能对某人有所帮助:-

import matplotlib.pyplot as plt
import time
import numpy as np
from openpyxl import load_workbook
import tkinter
import matplotlib.gridspec as gridspec


wb = load_workbook('C:\\Users\\A\\Desktop\\case1.xlsx')
sheet_1 = wb.get_sheet_by_name('case1')
# simulates input from serial port

plt.figure(figsize=(6, 4), facecolor='Grey')
G = gridspec.GridSpec(6, 2)

x = np.zeros(sheet_1.max_row)
y = np.zeros(len(x))
z = np.zeros(len(x))
a = np.zeros(len(x))
b = np.zeros(len(x))
c = np.zeros(len(x))
d = np.zeros(len(x))

for i in range(1, sheet_1.max_row):
    x[i] = sheet_1.cell(row=i + 1, column=2).value
    y[i] = sheet_1.cell(row=i + 1, column=4).value
    z[i] = sheet_1.cell(row=i + 1, column=6).value
    a[i] = sheet_1.cell(row=i + 1, column=7).value
    b[i] = sheet_1.cell(row=i + 1, column=8).value
    c[i] = sheet_1.cell(row=i + 1, column=9).value
    d[i] = sheet_1.cell(row=i + 1, column=10).value
    plt.ion()

#print(x[i])
#print("Printed ................")
list1 = []
list2 = []
list3 = []
list4 = []
list5 = []
list6 = []
list7 = []
#print("---------------------------------")
for i in range(0,len(x)):
    #print("Inside Loop ....")
    list1.append(x[i])

    list2.append(y[i])
    list3.append(z[i])
    list4.append(a[i])
    list5.append(b[i])
    list6.append(c[i])
    list7.append(d[i])
    #print(list1[i], list2[i], end=" ")
    time.sleep(1)
    plt.plot(list1, list2, color='cyan', label='HR')
    plt.plot(list1, list3, color='red', label='Pulse')
    plt.plot(list1, list4, color='yellow', label='Spo2')
    plt.plot(list1, list5, color='green', label='Perf')
    plt.plot(list1, list6, color='magenta', label='etCO2')
    plt.plot(list1, list7, color='pink', label='imCO2')
    plt.grid(True)
    plt.pause(0.5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM