-
Notifications
You must be signed in to change notification settings - Fork 1
/
step3_visualizing.py
78 lines (53 loc) · 1.71 KB
/
step3_visualizing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# coding: utf-8
# ### Shanghai Covid 2022
# - Data crawing
# - Geocoding
# - ***Visualizing***
# In[4]:
# libraries
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
import pandas as pd
import matplotlib.pyplot as plt
import descartes
import geopandas as gpd
from shapely.geometry import Point, Polygon
get_ipython().run_line_magic('matplotlib', 'inline')
# In[16]:
# -------------------------------------
# visualize the summary data of districts
# read the data
df = pd.read_csv('shanghai2022_04_04_districts.csv')
# plot
bar = (
Bar(init_opts=opts.InitOpts())
.add_xaxis(list(df.iloc[:,1]))
.add_yaxis('positive infectors', list(df.iloc[:,2]))
.add_yaxis('asymptomatic infectors', list(df.iloc[:,3]))
.set_global_opts(title_opts=opts.TitleOpts(title='Infection data of districts', subtitle = 'Shanghai on 04/04/2022'))
)
bar.render_notebook()
# In[17]:
print(df)
# In[5]:
# -------------------------------------
# visualize the infectors locations data
# first, import the .shp of Shanghai
# import the map
shanghai_map = gpd.read_file('./stanford-dv960kb3448-shapefile/shanghai1.shp')
# plot the empty map
fig,ax = plt.subplots(figsize = (15,15))
shanghai_map.plot(ax = ax, color = 'grey')
# read the data
df = pd.read_csv('shanghai2022_04_04.csv')
crs = {'init': 'epsg:4326'}
# creat points to be shown
geo = [Point(xy) for xy in zip(df['longitude'], df['latitude'])]
# gen geodataframe
geo_df = gpd.GeoDataFrame(df, crs=crs, geometry=geo)
fig,ax = plt.subplots(figsize = (15,15))
# mark on the map
shanghai_map.plot(ax = ax, color = 'lightgray')
geo_df.plot(ax = ax, markersize = 60, color = 'darkred', alpha = 0.2, marker = 'o')