Using mercator projection: I want to color only lands/countries, and all land(except Antarctica) plus all ocean gets colored
Code-Sage opened this issue · 2 comments
Code-Sage commented
This code is from Basemap docs pages, with some changing to focus on my issue at hand.
I want to color(in this case, magenta) all lands(Antarctica can be overlooked), and all ocean should be white like Antarctica is right now. The shapefile is from here
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import PatchCollection
from matplotlib.patches import PathPatch
import numpy as np
fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot(111)
map = Basemap(llcrnrlon=-180,llcrnrlat=-85,urcrnrlon=180.,urcrnrlat=85, projection='merc')
map.readshapefile('countries_lakes/ne_10m_admin_0_countries_lakes', 'units')
patches = []
for info, shape in zip(map.units_info, map.units):
patches.append(Polygon(np.array(shape), True))
ax.add_collection(PatchCollection(patches, facecolor= 'm', edgecolor='k', linewidths=1.))
plt.savefig('dem.png', bbox_inches='tight')
molinav commented
Hi, @Code-Sage! Sorry for being so late replying. I cannot reproduce your problem, what Python version and basemap
version are you using? Can you try out the following code snippet?
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.collections import PatchCollection
from matplotlib.patches import Polygon
from mpl_toolkits.basemap import __version__ as basemap_version
from mpl_toolkits.basemap import Basemap
print("Python version: ", sys.version)
print("Basemap version:", basemap_version)
fig = plt.figure(figsize=(20, 20))
ax = fig.add_subplot(111)
bmap = Basemap(projection="merc",
llcrnrlon=-180, llcrnrlat=-85,
urcrnrlon=+180, urcrnrlat=+85)
bmap.readshapefile("countries_lakes/ne_10m_admin_0_countries_lakes", "units")
patches = []
patches_kwds = dict(facecolor="m", edgecolor="k", linewidths=1.)
for info, verts_i in zip(bmap.units_info, bmap.units):
patches.append(Polygon(np.array(verts_i), closed=True))
ax.add_collection(PatchCollection(patches, **patches_kwds))
plt.savefig("dem.png", bbox_inches="tight")
It returns the following in my console:
Python version: 3.11.2 (tags/v3.11.2:878ead1, Feb 7 2023, 16:38:35) [MSC v.1934 64 bit (AMD64)]
Basemap version: 1.3.8
And it saves the following image (probably what you were expecting):