Python Forum
How to plot data from live datasource?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to plot data from live datasource?
#1
Hi,

Ive managed to get live data from the web in code below.
Now i try to make a graph from that data.
Im getting data in numbers and when i run the script an empty graph is showing.
I dont know what im doing wrong Cry

Thanks.


import matplotlib
import matplotlib.pyplot as plt
import requests
import json
import time
import numpy as np



url = 'https://api.darksky.net/forecast/19322f67baef0exxxxxx/xxx.483143,xxxx.622984'
response = requests.get(url)
data = response.json()
fields = ['time', 'temperature', 'pressure', 'humidity', 'precipProbability']
transformed = [
    {
        field: rec.get(field)
        for field
        in fields
    }
    for rec
    in data['hourly']['data']
]

plt.plot('temperature')
plt.show()
print(json.dumps(transformed, indent=4))
Reply
#2
Hi,

I managed to plot data with script below.
But i dont know how to plot time on the X axis.
The print time gives unix time, so that has to be converted to datetime to.
Thanks

import pandas as pd 
import requests
import json 
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime
import time

url = 'https://api.darksky.net/forecast/1f67baef0ec1a24e097666a982fb/5483143,32984?units=si'
response = requests.get(url)
weather_data = response.json()
time = [weather_data['hourly']['data'][k]['time'] for k in range(0,48)]
temperature_min = [weather_data['hourly']['data'][k]['temperature'] for k in range(0,48)]
temperature_max = [weather_data['hourly']['data'][k]['dewPoint'] for k in range(0,48)]


print(temperature_min)
print(time)




fig, ax = plt.subplots()
ax.plot(time, temperature_min, label='temp')
ax.plot(time, temperature_max)

ax.grid()
plt.show()
Reply
#3
Hi,

Im still struggling with above script how to plot, instead of unix time, local time.

Thanks.
Reply
#4
Hi,

I managed to get the time shown in my plot on the X axis.
But it is plotting 2 lines and connect the begin of the plot with the end of the plot by the looks of it, while i have selected to only plot time and humidity.
What have i done wrong in the code or am i missing code?

Thanks.

[Image: Screenshot-20200125-140932.png]


import pandas as pd 
import requests
import json 
import matplotlib.pyplot as plt
import numpy as np
import datetime
import time
import matplotlib

url = 'https://api.darksky.net/forecast/19322aef0ec1a24e097666a982fb/53143,3.284?units=si'
response = requests.get(url)
weather_data = response.json()
time =[weather_data['hourly']['data'][k]['time'] for k in range(0,48)]
temperature = [weather_data['hourly']['data'][k]['temperature'] for k in range(0,48)]
humidity = [weather_data['hourly']['data'][k]['humidity']*100 for k in range(0,48)]
precipProbability = [weather_data['hourly']['data'][k]['precipProbability'] for k in range(0,48)]
precipIntensity = [weather_data['hourly']['data'][k]['precipIntensity'] for k in range(0,48)]
summary = [weather_data['hourly']['data'][k]['summary'] for k in range(0,48)]

c=[datetime.datetime.fromtimestamp(x).strftime("%H:%M:%S") for x in time] 

print(c)
print(summary)
print(temperature)
print(humidity)

fig, ax = plt.subplots()
plt.ylim(0,100)
plt.plot(c, humidity,linewidth=1.0)
ax.grid()
plt.show()
Reply
#5
Hi,

The plotting is finally working ok.
Ive made a simular code from previous code but with other data updating every minute.
Ive ad a repeating execution of the code every minute.
Thats working ok, but i get a new plot figure every minute to.
So i am missing some code to have the plot updating every minute in the same figure...
Maybe someone is prepared to show me in the right direction code wise Smile

Thanks.


import pandas as pd 
import requests
import json 
import matplotlib.pyplot as plt
import numpy as np
import datetime
import time
import matplotlib
import schedule    
from time import sleep

starttime=time.time()

def task():

    url = 'https://api.darksky.net/forecast/19aef0ec1a24e097666a982fb/53143,3.984?units=si'
    response = requests.get(url)
    weather_data = response.json()
    time =[weather_data['minutely']['data'][k]['time'] for k in range(0,60)]
    precipProbability = [weather_data['minutely']['data'][k]['precipProbability'] for k in range(0,60)]
    precipIntensity = [weather_data['minutely']['data'][k]['precipIntensity'] for k in range(0,60)]

    c=[datetime.datetime.fromtimestamp(x).strftime("%Y-%m-%d %H:%M:%S") for x in time] 

    print(c)
    print(precipProbability)
    print(precipIntensity)

    fig, ax = plt.subplots()
    plt.ylim(0,1)
    plt.plot(c, precipProbability,linewidth=1.0)
    plt.plot(c, precipIntensity,linewidth=1.0)

    plt.xticks(fontsize=6, rotation=90)
    ax.grid()

    plt.show()

while True:
    try:
        task()
        time.sleep(60 - time.time() % 60)
        refresh()
    except:
        pass
        time.sleep(1)
else:
 time.sleep(60)







    
Reply
#6
Hi,

I still cant figure out how to get it to work properly Huh
Reply
#7
Hi,

Someone able to help me out with it Smile
Reply
#8
Cry
Reply
#9
Blush
Reply
#10
Huh
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Likert survey data plot error Andrzej_Andrzej 6 1,396 Jul-16-2023, 10:11 PM
Last Post: deanhystad
  Plot a pandas data fram via pyqtgraph with an modul import and qt designer widget Nietzsche 0 841 May-29-2023, 02:42 PM
Last Post: Nietzsche
  Create simple live plot of stock data dram 2 2,910 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
Thumbs Up Python 3 Jupyter notebook ternary plot data nicholas 0 930 Jan-21-2023, 05:01 PM
Last Post: nicholas
  Plot data from CSV allen04 2 2,381 Jan-03-2021, 10:30 AM
Last Post: Axel_Erfurt
  How to plot intraday data of several days in one plot mistermister 3 2,897 Dec-15-2020, 07:43 PM
Last Post: deanhystad
  Overwrite previous live data. Makada 2 2,354 Nov-07-2020, 07:40 PM
Last Post: Makada
  Creating Complex Color Spectrum Plot From Data JoeDainton123 2 2,117 Sep-15-2020, 08:09 AM
Last Post: DPaul
  Bode plot from time series experiment data discus 4 7,323 Jun-20-2020, 07:46 AM
Last Post: discus
  Python animate live plotting fetching data from Mysql Table dhirajm 6 3,624 Apr-24-2020, 05:07 PM
Last Post: dhirajm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020