Ethereum: How can I get live data from the Binance API and plot it on Matplotlib at the same time? I can do both separately, but not together

I can help you pull live data from the Binance API and plot it simultaneously using Matplotlib. However, I need to clarify that there are a few issues with your current code.

Firstly, the createFrame function is not defined anywhere in your code. You will need to define this function or replace it with an existing one that retrieves frame data from Binance.

Secondly, the socket object appears to be used for WebSocket connections, but Matplotlib does not have a built-in API for real-time plotting. However, you can use the matplotlib.ticker module to create automated tickers for your chart. This will allow you to display live data without having to constantly poll the Binance API.

Here is an example of how you can modify your code to achieve this:

import socket

import time

import matplotlib.pyplot as plt








Ethereum: How can I pull live data from Binance API and simultaneously plot it on Matplotlib? I can do both individually but not together

Define the function to retrieve frame data from Binance

def create_frame(msg):


Assuming msg is a bytes object, decode it first

frame = msg.decode('utf-8')

return frame

class BinanceClient:

def __init__(self):

self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

async def connect_to_binance(self, url):

await self.socket.connect((url, 443))

self.socket.settimeout(1)

Set a timeout for the connection

async def send_message(self, message):

await self.socket.recv()

frame = create_frame(msg)

return frame.decode('utf-8')


Create an instance of BinanceClient

client = BinanceClient()

async def main():

while True:


Connect to the Binance API and get the latest frame data

url = "

client.connect_to_binance(url)

msg = await client.send_message("GET ORDERBOOK")


Plot the received frame data using Matplotlib

frames = []

while True:

frame = create_frame(msg)

frames.append(frame.decode('utf-8'))

time.sleep(0.01)

Wait 10 milliseconds

plt.figure(figsize=(20, 10))

plt.plot(frames)

plt.title("Binance Frame Data")

plt.xlabel("Time")

plt.ylabel("Frame Data")

plt.show()


Get the latest frame data from Binance

msg = wait client.send_message("GET ORDERBOOK")

if __name__ == "__main__":

play()

This code creates an instance of BinanceClient and uses it to connect to the Binance API. It then plots the received frame data using Matplotlib.

Note that this is a simplified example and you may need to adjust the connection timeout and graph interval to suit your specific needs. Additionally, you will need to replace the create_frame function with an existing one that retrieves Binance frame data.

Also, keep in mind that plotting real-time data requires periodic requests to the server, which can be resource-intensive. You may want to consider using a more efficient data streaming API if you plan on handling large volumes of data.

اترك ردّاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *