Newbie question here...
I am trying to make a map of Census data using the geometry shapefiles they provide (via NHGIS).
Here is what the geometry column looks like:
When I make a simple maptlotlib map using this code:
# make a map from final_gdf
# import the colors library (again)
import matplotlib.pyplot as plt
import matplotlib.colors as colors
# Define the colors for the minimum and maximum values, these are set here to light gray and bright blue
min_color = '#E2E3E4'
max_color = '#0BB4FF'
# Create a colormap and plot the values in column AQP6001
cmap = colors.LinearSegmentedColormap.from_list("", [min_color, max_color])
# Create the map, using aspect = 1 to ensure the map is not distorted
fig, ax = plt.subplots(1, 1)
final_gdf.plot(column='AQP6E001', ax=ax, legend=True, cmap=cmap, aspect=1, vmin=50000, vmax=150000)
plt.show()
it looks great:
I want to add a simple basemap to show geographic boundaries, etc. I know the geometry must be in 3857 format in order to add a basemap (right?), but is currently in 4326:
final_gdf.crs
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree) Area of Use:
- name: World.
- bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 ensemble
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
But when I convert the data to crs 3857, the geometry data turns into POLYGON ((inf inf, inf inf, inf inf, inf inf, ...
The code I am using for the conversion is:
final_gdf_3857 = final_gdf.to_crs(epsg=3857)
I've also tried this below:
final_gdf_3857 = gpd.GeoDataFrame(final_gdf_nyc, geometry='geometry', crs='EPSG:3857')
This second method results in no change to the geometry column (ie, they are still the original coordinates), and unsurprisingly results in a pretty nonsensical basemap, with none of the correct geographic features:
ax2 = final_gdf_3857.plot(figsize=(10, 10), alpha=0.5, edgecolor="k") cx.add_basemap(ax, zoom=10)
Clearly, the conversion from 4326 to 3857 is not working correctly. Any ideas?