I am trying to obtain and plot real-time data from Binance (ETHUSDT) using a WebSocket. Getting the data is no problem, but I cannot get a realtime plot to work when using matplotlib.
In the code, I update the close prices each time a message is received and try to plot this data in realtime. The plot is shown on the screen but blocks further code execution. I have to manually close the plot for the next message to be received.
My question is: how can I plot the data in realtime without blocking the code?
import websocket, json
import matplotlib.pyplot as plt
import numpy as np
TRADE_SYMBOL = "ETHUSDT"
SOCKET = "wss://stream.binance.com:9443/ws/ethusdt@kline_1m"
closes = np.array([])
# CREATING AXIS
plt.axis([0, 1000, 0, 1])
def on_message(ws, message):
global closes
message = json.loads(message)
candle = message['k']
close = candle['c']
closes = np.append(closes, float(close))
# PLOTTING HERE
plt.plot(closes)
plt.show()
ws = websocket.WebSocketApp(SOCKET, on_message=on_message)
ws.run_forever()