I am very new to python so I am sure my code looks a bit elementary. I have a vacuum chamber that I can communicate with via TCP commands. My goal for now is to plot pressure on one graph and thermocouple temperatures on another. I am able to communicate fine and read data back just fine, and I can plot the live data to the second graph, but when I try to plot data to the first graph it doesn't show.
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()
I have changed the code to show 3 graphs to prove it is only the last graph I can plot to and this does seem to be the case. I am sure it is a simple fix but I am at a point where I am completely stuck. Any help would be much appreciated, thanks!