I am trying to make a map with several layers and a light grey background basemap from carto DB positron.
This code works (no basemap), all crs set to "EPSG:5070":
import matplotlib.pyplot as plt
import geopandas as gpd
import matplotlib.patches as mpatches
# Get the bounds of Illinois to set the plot limits
bounds = IL_boundary.total_bounds
# Create the plot
fig, ax = plt.subplots()
# Plot the Illinois exterior boundary with an orange outline
ax = IL_boundary.boundary.plot(ax=ax, edgecolor='orange', linewidth=.5)
# Set the map extent to Illinois
ax.set_xlim([bounds[0], bounds[2]]) # Set xlim to match Illinois boundary
ax.set_ylim([bounds[1], bounds[3]]) # Set ylim to match Illinois boundary
# Plot highways in grey
high_shp.plot(ax=ax, color='grey', label='Major highways', linewidth=0.4)
# Plot hospitals in red
hosp_shp.plot(ax=ax, color='blue', label='Major hospitals', markersize=10)
# Add a title
ax.set_title("Major highways and hospitals in Illinois")
# Remove latitude and longitude tick marks
ax.set_xticks([])
ax.set_yticks([])
# Create legend manually
highway_patch = mpatches.Patch(color='grey', label='Major highways')
hospital_patch = mpatches.Patch(color='blue', label='Major hospitals')
plt.legend(handles=[highway_patch, hospital_patch], fontsize=8, loc='lower left')
plt.tight_layout()
# Show the combined figure
plt.show()
Adding a basemap causes the highways and hospitals to disappear, but not the Illinois boundary. I reproject to 3857 for contextily (I have tried this and the CRS above) and set the boundaries to the limits of the IL_boundary.
import matplotlib.pyplot as plt
import geopandas as gpd
import contextily as ctx # For adding a basemap
import matplotlib.patches as mpatches
# Reproject high_shp, hosp_shp, and IL_boundary
high_shp = high_shp.to_crs(epsg=3857)
hosp_shp = hosp_shp.to_crs(epsg=3857)
IL_boundary = IL_boundary.to_crs(epsg=3857)
# Create the plot
fig, ax = plt.subplots()
# Plot the Illinois exterior boundary with an orange outline
ax = IL_boundary.boundary.plot(ax=ax, edgecolor='orange', linewidth=.5)
# Add a basemap
ctx.add_basemap(ax, crs=IL_boundary.crs.to_string(), source=ctx.providers.CartoDB.Positron, zoom=10)
# Plot highways in grey
high_shp.plot(ax=ax, color='grey', label='Major Highways', linewidth=0.4)
# Plot hospitals in red
hosp_shp.plot(ax=ax, color='blue', label='Major Hospitals', markersize=10)
# Add a title
ax.set_title("Major highways and hospitals in Illinois")
# Remove latitude and longitude tick marks
ax.set_xticks([])
ax.set_yticks([])
# Create legend manually
highway_patch = mpatches.Patch(color='grey', label='Major highways')
hospital_patch = mpatches.Patch(color='blue', label='Major hospitals')
plt.legend(handles=[highway_patch, hospital_patch], fontsize=8, loc='lower left')
plt.tight_layout()
# Show the combined figure
plt.show()