I am using an API which returns a dictionary containing a key whose value keeps changing every second.
This is the API call link which returns the data in this form.
{"symbol":"ETHUSDT","price":"588.15000000"}
Here.. the key "price" keeps changing its value every second.
This is my Python scrips which appends this value in a list.
import requests
import matplotlib.pyplot as plt
url = "https://api.binance.com/api/v3/ticker/price?symbol=ETHUSDT"
def get_latest_crypto_price():
r = requests.get(url)
response_dict = r.json()
return float(response_dict['price'])
def main():
last_price = 10
eth_price =[]
while True:
price = get_latest_crypto_price()
if price != last_price:
eth_price.append(price)
fig, ax = plt.subplots()
ax.plot(eth_price)
plt.show()
last_price = price
main()
My Question?
My code does plot the line but every time I have to close the Plot Window and it reopens again showing the new appended value. I know this is happening because my loop starts again after appending each value.
I am struck as to how to plot this in real time. Kindly help me in correcting my script.
Thank You
