See this article on my own blog https://dyingdown.github.io/2020/02/06/Draw-map/
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
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
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
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
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)
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)
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/