我对 Python 还很陌生,所以我的代码看起来有点简单。我有一个真空室,我可以通过 TCP 命令与之通信。我现在的目标是在一个图表上绘制压力,然后
我对 Python 还很陌生,所以我确信我的代码看起来有点初级。我有一个真空室,我可以通过 TCP 命令与之通信。我现在的目标是在一张图上绘制压力,在另一张图上绘制热电偶温度。我可以很好地进行通信并很好地读回数据,我可以将实时数据绘制到第二张图中,但是当我尝试将数据绘制到第一张图中时,它没有显示。
import socket
import sys
import time
import binascii
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib.animation import FuncAnimation
from datetime import datetime
matplotlib.use('Qt5Agg')
#TVAC IP and Port
target_host = "10.1.2.101"
target_port = 1
print()
#trying to open a TCP socket
try:
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host,target_port))
except socket.error:
print("Socket Not Created! ¯\(°_o)/¯")
sys.exit()
print("Socket Created (⌐⊙_⊙)")
print()
print("Socket Connected To " + target_host)
print()
time.sleep(2)
Pressure, TC0, TC1, TC2, TC3, TC4, TC5, TC6, TC7 = "?VP", "?T2", "?T12", "?T13", "?T14", "?T15", "?T16", "?T17", "?T18"
#carriage return
CR = "\r"
def TVAC(cmd):
try:
#send command and receive data ex/ VP:3.23e-07\r
client.send(bytes(cmd + CR + '\0','ascii'))
response = client.recv(1024).decode('ascii')
if (cmd == '?VP'):
response4 = float(response[3:-1])
response5 = format(response4, '.000000000008f')
else:
response2 = response[5:-1]
response3 = float(response2)
response5 = np.divide(response3, 10)
response6 = float(response5)
print(response6)
except socket.error:
print("Failed to send command")
return response6
#animate the data on a plot graph
fig = plt.figure()
axis1 = fig.add_subplot(211)
axis2 = fig.add_subplot(212)
#figure, (axis1, axis2) = plt.subplots(2)
x_data, y_data = [], []
x_data1, y_data2, y_data3, y_data4, y_data5, y_data6, y_data7, y_data8, y_data9 = [], [], [], [], [], [], [], [], []
line1, = axis1.plot_date(x_data, y_data, "-", color='red')
line2, = axis2.plot_date(x_data1, y_data2, "-", color='blue')
line3, = axis2.plot_date(x_data1, y_data3, "-", color='green')
line4, = axis2.plot_date(x_data1, y_data4, "-", color='purple')
line5, = axis2.plot_date(x_data1, y_data5, "-", color='yellow')
line6, = axis2.plot_date(x_data1, y_data6, "-", color='black')
line7, = axis2.plot_date(x_data1, y_data7, "-", color='orange')
line8, = axis2.plot_date(x_data1, y_data8, "-", color='grey')
line9, = axis2.plot_date(x_data1, y_data9, "-", color='red')
line = [line1, line2, line3, line4, line5, line6, line7, line8, line9]
axis1.grid(axis = 'y')
axis2.grid(axis = 'y')
def update(frame):
xdata = datetime.now()
xdata1 = datetime.now()
ydata = TVAC(Pressure)
ydata1 = TVAC(TC0)
ydata2 = TVAC(TC1)
ydata3 = TVAC(TC2)
ydata4 = TVAC(TC3)
ydata5 = TVAC(TC4)
ydata6 = TVAC(TC5)
ydata7 = TVAC(TC6)
ydata8 = TVAC(TC7)
x_data.append(xdata)
x_data1.append(xdata1)
y_data.append(ydata)
y_data2.append(ydata1)
y_data3.append(ydata2)
y_data4.append(ydata3)
y_data5.append(ydata4)
y_data6.append(ydata5)
y_data7.append(ydata6)
y_data8.append(ydata7)
y_data9.append(ydata8)
#axis1.plot(x_data, y_data)
line[0].set_data(x_data, y_data)
line[1].set_data(x_data1, y_data2)
line[2].set_data(x_data1, y_data3)
line[3].set_data(x_data1, y_data4)
line[4].set_data(x_data1, y_data5)
line[5].set_data(x_data1, y_data6)
line[6].set_data(x_data1, y_data7)
line[7].set_data(x_data1, y_data8)
line[8].set_data(x_data1, y_data9)
axis1.set_title("Pressure")
axis2.set_title("Temperature")
fig.gca().relim()
axis1.ticklabel_format(axis='y',style='sci',scilimits=(0,0))
axis2.ticklabel_format(axis='y',style='plain',scilimits=(0,0))
fig.gca().autoscale_view()
return line,
anim = FuncAnimation(fig, update, cache_frame_data=False)
plt.show()
我已将代码更改为显示 3 个图表,以证明这只是我可以绘制的最后一个图表,情况似乎确实如此。我确信这是一个简单的修复,但我现在完全陷入困境。任何帮助都将不胜感激,谢谢!