'Visualize data on realtime via socket is too slow with matplotlib
I want to plot a data that received from a sever via socket. I will parse the data on client. For example, data is 0 1 2 indicates that 0 is index, 1 is data of sub figure 0 and 2 is data of sub figure 1. With the bellow code, I can visualize data using matplotlib on realtime, however, the visualization becomes very slow after 10 seconds, and it looks like that it works on main thread (mean I cannot open other task). Could you suggest to me any solution to speed up the visualization or do it in background thread?
import socket
import os
import numpy as np
import matplotlib.pyplot as plt
# Create a socket object
s = socket.socket()
# Define the port on which you want to connect
port = 8080
# connect to the server on local computer
s.connect(('127.0.0.1', port))
fig, axs = plt.subplots(2, 2)
fig.tight_layout()
subfig1_data = []
subfig2_data = []
x_data = []
while(1):
# receive data from the server and decoding to get the string.
message_received = s.recv(1024).decode()
# 0 1 2
data = message_received.split(" ")
x_data.append(int(data[0]))
subfig1_data.int(data[1])
subfig2_data.int(data[2])
axs[0, 0].plot(x_data, subfig1_data,'-o',alpha=0.8, color="blue")
axs[0, 1].plot(x_data, subfig2_data,'-o',alpha=0.8, color="green")
fig.show()
plt.pause(0.00001)
# close the connection
s.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
