You are not logged in.
If you are a current student, please
Log In for full access to this page.
Table of Contents
1) ผังแผนที่โลก
ลงเครื่องมือ geopandas เพิ่มสำหรับแผนที่
!pip install geopandas plotly
import plotly.express as px
df = px.data.gapminder()
df.head()
|
country |
continent |
year |
lifeExp |
pop |
gdpPercap |
iso_alpha |
iso_num |
0 |
Afghanistan |
Asia |
1952 |
28.801 |
8425333 |
779.445314 |
AFG |
4 |
1 |
Afghanistan |
Asia |
1957 |
30.332 |
9240934 |
820.853030 |
AFG |
4 |
2 |
Afghanistan |
Asia |
1962 |
31.997 |
10267083 |
853.100710 |
AFG |
4 |
3 |
Afghanistan |
Asia |
1967 |
34.020 |
11537966 |
836.197138 |
AFG |
4 |
4 |
Afghanistan |
Asia |
1972 |
36.088 |
13079460 |
739.981106 |
AFG |
4 |
px.choropleth(
df,
locations="iso_alpha",
color="lifeExp",
hover_name="country",
animation_frame="year",
range_color=[20,80]
)
2) ผังแผนที่ประเทศไทย
top
2.1) การพลอต scatter บนแผนที่ (scatter_geo)
import pandas as pd
pv = pd.read_csv("https://raw.githubusercontent.com/kongruksiamza/PythonWebMap/master/province_th.csv")
pv.head()
|
Number |
City |
LAT |
LON |
ZOOM |
0 |
1 |
กรุงเทพมหานคร |
13.727896 |
100.524123 |
13 |
1 |
2 |
กระบี่ |
8.086300 |
98.906283 |
13 |
2 |
3 |
กาญจนบุรี |
14.022780 |
99.532811 |
13 |
3 |
4 |
กาฬสินธุ์ |
16.431408 |
103.505876 |
13 |
4 |
5 |
กำแพงเพชร |
16.482780 |
99.522662 |
13 |
fig = px.scatter_geo(pv, lat="LAT", lon="LON", hover_name="City")
fig.update_geos(fitbounds="locations")
2.2) การพลอตรูปร่างบนแผนที่ (choropleth)
import geopandas as gpd
df = gpd.read_file('https://bit.ly/thaigeojson').set_index("tname")
df.head()
|
code |
name |
area |
geometry |
tname |
|
|
|
|
กรุงเทพมหานคร |
10 |
Bangkok |
1568.95 |
POLYGON ((100.85537 13.69198, 100.71321 13.711... |
สมุทรปราการ |
11 |
Samut Prakan |
960.09 |
POLYGON ((100.85537 13.69198, 100.94033 13.657... |
นนทบุรี |
12 |
Nonthaburi |
633.25 |
POLYGON ((100.35029 14.11371, 100.33659 14.060... |
ปทุมธานี |
13 |
Pathum Thani |
1520.06 |
POLYGON ((100.91242 14.21391, 100.90529 13.955... |
พระนครศรีอยุธยา |
14 |
Phra Nakhon Si Ayutthaya |
2534.07 |
POLYGON ((100.59405 14.65593, 100.57436 14.589... |
fig = px.choropleth(df, geojson=df.geometry, locations=df.index, color="area")
fig.update_geos(fitbounds="locations")
2.3) รวม scatter บนรูปร่างบนแผนที่
fig = px.choropleth(df, geojson=df.geometry, locations=df.index, color="area")
fig.add_scattergeo(lat=pv.LAT, lon=pv.LON, hovertext=pv.City, name="Province")
fig.update_geos(fitbounds="locations")
3) รายละเอียดเพิ่มเติม
top