python matplotlib实现动态实时温度曲线

现在有个项目需要实时显示温度,可以直接用pyqtgraph实现,但是界面和曲线的调整实在不会,用了一段时间后放弃了。关于pyqtgraph实时绘图的代码和效果图如下。python matplotlib实现动态实时温度曲线_第1张图片

 

import time
import temp_calcu_3_onestep
import calib_drift_2_onestep
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
import temp_calcu_2
win = pg.GraphicsLayoutWidget(show=True)
win.setWindowTitle('点位温度折线图')
p2 = win.addPlot()
data1 = np.empty(50)
data2 = np.empty(50)
data3 = np.empty(50)
data4 = np.empty(50)
curve2 = p2.plot(data1,)
curve3 = p2.plot(data2)
curve4 = p2.plot(data3, pen=(0,255,0))
curve5 = p2.plot(data4, pen=(255,0,0))

# list_temp = temp_calcu_3_onestep.CH1_temp()


ptr1 = 0
def update1():

    global data1, data2, ptr1,data3
    list_wave = calib_drift_2_onestep.after_wave()
    data1[:-1] = data1[1:]  # shift data in the array one sample left
    data2[:-1] = data2[1:]
    data3[:-1] = data3[1:]
    data4[:-1] = data4[1:]
    data1[-1] = temp_calcu_3_onestep.CH1_temp(list_wave)[0]         #规定数据来源
    data2[-1] = temp_calcu_3_onestep.CH1_temp(list_wave)[1]
    data3[-1] = temp_calcu_3_onestep.CH1_temp(list_wave)[2]         #不同通道的温度
    data4[-1] = temp_calcu_3_onestep.CH1_temp(list_wave)[3]
    ptr1 += 1
    curve2.setData(data1)
    curve2.setPos(ptr1, 0)
    curve3.setData(data2)
    curve3.setPos(ptr1, 0)
    curve4.setData(data3)
    curve4.setPos(ptr1, 0)
    curve5.setData(data4)
    curve5.setPos(ptr1, 0)
# 3) Plot in chunks, adding one new plot curve for every 100 samples
chunkSize = 100
# Remove chunks after we have 10
maxChunks = 10
startTime = pg.ptime.time()
win.nextRow()
curves = []
def update():
    update1()
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)
if __name__ == '__main__':
    pg.exec()

然后试了下matplotlib,横坐标不能自动变化,这个我还在继续。代码和效果图如下所示。

from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
import matplotlib.animation as animation
import datetime as dt
import calib_drift_2_onestep
import temp_calcu_3_onestep
plt.style.use('seaborn-pastel')
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
xs = []
ys = []

def animate(i, xs, ys):
    list_wave = calib_drift_2_onestep.after_wave()
    xs.append(dt.datetime.now().strftime('%H:%M:%S'))
    ys.append(temp_calcu_3_onestep.CH1_temp(list_wave)[0])
    xs = xs[-20:]
    ys = ys[-20:]

    ax.clear()
    ax.plot(xs,ys)

    plt.xticks(rotation=45, ha='right')
    plt.subplots_adjust(bottom=0.30)
    plt.title('wendu')
    plt.ylabel('dangqianwendu')

ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1000)
plt.show()

python matplotlib实现动态实时温度曲线_第2张图片

 

你可能感兴趣的:(python_工程,python)