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

I can help you get live data from Binance API and plot it using Matplotlib at the same time. However, I have to clarify that there are some issues with the current code.

First of all, the function “createFrame” is not defined anywhere in the code. You need to define this function or replace it with an existing function that reads frame data from Binance.

Second, it seems that the “socket” object is 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 automatic tickers for your plots. This allows you to display live data without having to constantly request the Binance API.

Here is an example of how you can change the code to do 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 a function to read the frame data from Binance

def create_frame(msg):


Assuming the message is a byte object, decode it first

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

return frame

BinanceClient class:

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 the connection timeout

async def send_message(self, msg):

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 Binance API and get the latest frame data

url = "

client.connect_to_binance(url)

msg = expect client.send_message("BUY ORDER BOOK")


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 = expect client.send_message("BUY ORDER BOOK")

if __name__ == "__main__":

main ()

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

Please note that this is a simplified example and you may need to adjust the connection timeout and print interval to suit your needs. Additionally, you need to replace the “create_frame” function with an existing function that reads the frame data from the Binance system.

Also keep in mind that plotting live data requires periodic requests to the server, which can be resource-intensive. If you plan to handle large amounts of data, you may want to consider using a more efficient data streaming API.

Ethereum Transactions With Bitcoin

Leave a Comment

Your email address will not be published. Required fields are marked *