采用Plotly展示
Contents
采用Plotly展示¶
从数据库查询¶
我们将之前保存的全国主要城市的天气信息从数据库中读取。
import os
import sys
module_path = os.path.abspath(os.path.join('../..'))
print(module_path)
if module_path not in sys.path:
sys.path.append(module_path)
C:\Users\renb\PycharmProjects\weather_dashapp
from weather_book.weather_app.models.db_models import engine,WeatherInfo
import pandas as pd
df = pd.read_sql_table(WeatherInfo.__tablename__,engine)
df.shape
(89664, 14)
这里我们仅仅展示一个时刻的信息,所以我们需要对每个城市的数据“去重”,仅仅保留最后一个数据。
需要强调的是:如果数据库比较乱,每个城市最后一个数据对应的时间戳并不相同。
df.drop_duplicates(subset=['city'], keep='last',inplace=True)
df.shape
(1237, 14)
df.head()
cloudcover | lifted_index | prec_type | prec_amount | temp2m | rh2m | weather | timestamp | wind_direction | wind_speed | longitude | latitude | city | id | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
5823 | 1 | 15 | none | 0 | 9 | 12 | clearday | 2022-02-10 06:00:00 | W | 3 | 121.4667 | 31.1667 | Shanghai | 63.0 |
5887 | 2 | 15 | rain | 4 | 4 | 31 | rainday | 2022-02-10 06:00:00 | NW | 2 | 113.2590 | 23.1288 | Guangzhou | 127.0 |
5951 | 1 | 15 | none | 0 | 5 | 10 | clearday | 2022-02-10 06:00:00 | NW | 4 | 116.3914 | 39.9050 | Beijing | 191.0 |
6015 | 1 | 15 | none | 0 | 7 | 11 | clearday | 2022-02-10 06:00:00 | NW | 4 | 114.0540 | 22.5350 | Shenzhen | 255.0 |
6079 | 9 | 15 | none | 3 | 11 | 47 | cloudyday | 2022-02-10 06:00:00 | S | 2 | 112.5292 | 32.9987 | Nanyang | 319.0 |
import plotly.graph_objects as go
import plotly.offline as pyo
pyo.init_notebook_mode()
fig = go.Figure(go.Densitymapbox(lat=df.latitude, lon=df.longitude, z=df.cloudcover,
radius=20))
fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=112,mapbox_center_lat=28,mapbox_zoom=3)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()