1

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!

2
  • 1
    Please simplify the code to a minimal reproducible example. That means you should get rid of everything that is unrelated to the question at hand and provide data to test with (or use a random number generator to create fake data). One should be able to copy the minimal reproducible example straight into an IDE and run it. Commented Mar 8, 2024 at 15:01
  • 1
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a minimal reproducible example. Commented Mar 8, 2024 at 17:01

1 Answer 1

0

Since we don't have the data you're trying to plot, it's difficult to troubleshoot. Rewriting with randomly generated data will make it easier to troubleshoot plotting.

Here is a minimal recreation of what I think you're trying to do.

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(211)
ax2 = fig.add_subplot(212)


def update(frame):
    
    # '10' here is # of points to start w/;
    # frame increments by 1 every loop - plotting more data.
    x_data = np.arange(10+frame)
    
    # sets fixed seed, generates random 'pressure' data
    np.random.seed(0)
    p_data = np.random.rand(10+frame)
    
    # sets fixed seed, generates random 'temp' data.
    np.random.seed(1)
    tc_data = np.random.rand(10+frame)
    
    # plots data on two subplots
    ax1.plot(x_data, p_data)
    ax2.plot(x_data, tc_data)


anim = FuncAnimation(fig, update)
plt.show()

MatPlotLib's animation documentation (looks like you've already read, but for anybody in the future): https://matplotlib.org/stable/users/explain/animations/animations.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.