# How to color an area on a global map using python and basemap ?

Published: June 07, 2019

Examples of how to color an area on a global map using python and basemap:

### Plot a rectangular area

Using the function polygon (source: simple_basemap_example.py)

````from mpl_toolkits.basemap import Basemap`
`from matplotlib.patches import Polygon`
`from matplotlib.collections import PatchCollection`

`import numpy as np`
`import matplotlib.pyplot as plt`

`fig = plt.figure()`
`ax=fig.add_axes([0.1,0.1,0.8,0.8])`

`m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90, `
`                llcrnrlon=-180,urcrnrlon=180,resolution='c')`

`m.drawcoastlines()`
`m.drawparallels(np.arange(-90,90,30),labels=[1,1,0,1], fontsize=8)`
`m.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1], rotation=45, fontsize=8)`

`patches = []`
`zone_A = np.array([[-138,60],[-122,60],[-122,30],[-138,30]])`

`patches.append(Polygon(zone_A))`

`ax.add_collection(PatchCollection(patches, facecolor='lightgreen', edgecolor='k', linewidths=0.5))`

`plt.title('How to color a rectangular area on a worl map using Basemap ?', fontsize=8)`

`plt.xlabel('Longitude', labelpad=40, fontsize=8)`
`plt.ylabel('Latitude', labelpad=40, fontsize=8)`

`plt.savefig('plot_world_map_using_matplotlib_03.png', bbox_inches='tight')`
```

### Plot two rectangular areas

Another example:

````from mpl_toolkits.basemap import Basemap`
`from matplotlib.patches import Polygon`
`from matplotlib.collections import PatchCollection`

`import numpy as np`
`import matplotlib.pyplot as plt`

`fig = plt.figure()`
`ax=fig.add_axes([0.1,0.1,0.8,0.8])`

`m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90, `
`                llcrnrlon=-180,urcrnrlon=180,resolution='c')`

`m.drawcoastlines()`
`m.drawparallels(np.arange(-90,90,30),labels=[1,1,0,1], fontsize=8)`
`m.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1], rotation=45, fontsize=8)`

`patches = []`
`zone_A = np.array([[-138,60],[-122,60],[-122,30],[-138,30]])`
`zone_B = np.array([[-100,10],[-80,10],[-80,-20],[-100,-20]])`

`patches.append(Polygon(zone_A))`
`patches.append(Polygon(zone_B))`

`ax.add_collection(PatchCollection(patches, facecolor='lightgreen', edgecolor='k', linewidths=0.5))`

`plt.title('How to color an area on a global map with python and basemap ?', fontsize=8)`

`plt.xlabel('Longitude', labelpad=40, fontsize=8)`
`plt.ylabel('Latitude', labelpad=40, fontsize=8)`

`plt.savefig('plot_world_map_using_matplotlib_04.png', bbox_inches='tight')`
```

### Plot a triangular area

Example with the bermuda-triangle:

````from mpl_toolkits.basemap import Basemap`
`from matplotlib.patches import Polygon`
`from matplotlib.collections import PatchCollection`

`import numpy as np`
`import matplotlib.pyplot as plt`

`fig = plt.figure()`
`ax=fig.add_axes([0.1,0.1,0.8,0.8])`

`m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90, `
`                llcrnrlon=-180,urcrnrlon=180,resolution='c')`

`m.drawcoastlines()`
`m.drawparallels(np.arange(-90,90,30),labels=[1,1,0,1], fontsize=8)`
`m.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1], rotation=45, fontsize=8)`

`patches = []`
`zone_A = np.array([[-80.226529,25.78910],[-66.1057427,18.4663188],[-64.781380,32.294887]])`

`patches.append(Polygon(zone_A))`

`ax.add_collection(PatchCollection(patches, facecolor='red', edgecolor='k', linewidths=0.5))`

`plt.title('How to color an area on a global map with python and basemap ?', fontsize=8)`

`plt.xlabel('Longitude', labelpad=40, fontsize=8)`
`plt.ylabel('Latitude', labelpad=40, fontsize=8)`

`plt.savefig('plot_world_map_using_matplotlib_05.png', bbox_inches='tight')`
```

### Plot a circular area

Plot a circular area with Paris as orgine:

````from mpl_toolkits.basemap import Basemap`
`from matplotlib.patches import Polygon`
`from matplotlib.collections import PatchCollection`

`import numpy as np`
`import matplotlib.pyplot as plt`
`import math`

`fig = plt.figure()`
`ax=fig.add_axes([0.1,0.1,0.8,0.8])`

`m = Basemap(projection='cyl',llcrnrlat=-90,urcrnrlat=90, `
`                llcrnrlon=-180,urcrnrlon=180,resolution='c')`

`m.drawcoastlines()`
`m.drawparallels(np.arange(-90,90,30),labels=[1,1,0,1], fontsize=8)`
`m.drawmeridians(np.arange(-180,180,30),labels=[1,1,0,1], rotation=45, fontsize=8)`

`patches = []`

`center = (2.35,48.85)`
`radius = 10`

`nb_points = 20`

`zone_A = []`
`for i in range(nb_points):`
`    zone_A.append((center[0]+radius * math.cos(2.*i*math.pi/nb_points),center[1]+radius*math.sin(2.*i*math.pi/nb_points)))`

`patches.append(Polygon(zone_A))`

`ax.add_collection(PatchCollection(patches, facecolor='red', edgecolor='k', linewidths=0.5))`

`plt.title('How to color an area on a global map with python and basemap ?', fontsize=8)`

`plt.xlabel('Longitude', labelpad=40, fontsize=8)`
`plt.ylabel('Latitude', labelpad=40, fontsize=8)`

`plt.savefig('plot_world_map_using_matplotlib_06.png', bbox_inches='tight')`
```

Image

of