ORIGIN

Draw maps

Python 2 mins328 words

Preparation

Before you start to draw maps with python, you first need to install basemap and pyshp. Click here to see how to install basemap . And the following command to install pyshp

1
pip install pyshp --user

Draw maps

Since basemap is a plug-in in Python, so we first new a py file.

World map with coastlines

1
2
3
4
5
6
7
8
import matplotlib.pyplot as plot
from mpl_toolkits.basemap import Basemap

plot.figure(figsize=(16,8))
m = Basemap()
m.drawcoastlines()

plot.show()

World map with countries

1
2
3
4
5
6
7
8
9
import matplotlib.pyplot as plot
from mpl_toolkits.basemap import Basemap

plot.figure(figsize=(14,6))
m = Basemap()
m.drawcoastlines()
m.drawcountries(linewidth=1)

plot.show()

Chinese map board

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import matplotlib.pyplot as plot
from mpl_toolkits.basemap import Basemap

plot.figure(figsize=(14,6))
m = Basemap(
llcrnrlon=73.55770111084013,
llcrnrlat=18.159305572509766,
urcrnrlon=134.773925782502,
urcrnrlat=53.56085968017586
)
m.drawcoastlines()
m.drawcountries(linewidth=1)

plot.show()

Chinese map with province

Download the provinces information here http://www.diva-gis.org/gdata

(Don’t forget Taiwan)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import matplotlib.pyplot as plot
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon

plot.figure(figsize=(14,6))
m = Basemap(
llcrnrlon=73,
llcrnrlat=18,
urcrnrlon=134,
urcrnrlat=53
)
m.drawcoastlines()
m.drawcountries(linewidth=1)

m.readshapefile(r'CHN_adm/CHN_adm1', 'states', drawbounds=True)
m.readshapefile(r'TWN_adm/TWN_adm1', 'states', drawbounds=True)

ax = plot.gca()

plot.show

Well… It looks not so good.

It can be modified…

Chinese map with province (modified)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import matplotlib.pyplot as plot
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon

plot.figure(figsize=(14,6))
m = Basemap(
llcrnrlon=77,
llcrnrlat=14,
urcrnrlon=140,
urcrnrlat=51,
projection='lcc',
lat_1=33,
lat_2=45,
lon_0=100
)

m.readshapefile(r'CHN_adm/CHN_adm1', 'states', drawbounds=True)
m.readshapefile(r'TWN_adm/TWN_adm1', 'states', drawbounds=True)

ax = plot.gca()

plot.show()

Better.

Next, we are going to color the map to make it more beautiful!

https://dyingdown.github.io/2020/02/13/Draw-map-with-Color/

TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|