Python Forum

Full Version: How to plot data from live datasource?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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))
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()
Hi,

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

Thanks.
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()
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)







    
Hi,

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

Someone able to help me out with it Smile
Cry
Blush
Huh
Pages: 1 2