1

I would like to use cartopy to display a map of the cities in the contiguous United States. In order to do so, I have written the following code (the data comes from here):

import geopandas as gpd
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

cities = gpd.read_file("./data/citiesx010g.shp")
cities_m = cities[cities['STATE'].map(lambda s: s not in ['PR', 'AK', 'HI', 'VI'])]

fig = plt.figure()
ax = plt.subplot(111, projection=ccrs.AlbersEqualArea())
ax.scatter([p.x for p in cities_m.geometry], [p.y for p in cities_m.geometry], transform=ccrs.PlateCarree())
plt.show()

This is the output:

enter image description here

This leads to the following three questions:

  1. In this case, what would I have to do to "flip" this plot right side up?
  2. In the general case of having some kind of data in a AlbersEqualArea projection, what would I have to do to "flip" that data right side up?
  3. In the most general case of having just about any projection, how would I go about "flipping" the data right side up? (only the naive PlateCarree always comes out "right").
2

This effect occurs because by default points you draw are rendered as though they are on a globe or map, rendered in the current projection, centered on the point (0, 0). This map then gets "cut" to fit the points in your display.

This means that only points near (0, 0) will look "right".

You can adjust this center by specifying central_latitude and central_longitude parameters. Here's the corrected code:

import geopandas as gpd
import cartopy.crs as ccrs
import matplotlib.pyplot as plt

cities = gpd.read_file("./data/citiesx010g.shp")
cities_m = cities[cities['STATE'].map(lambda s: s not in ['PR', 'AK', 'HI', 'VI'])]

fig = plt.figure()
ax = plt.subplot(111, projection=ccrs.AlbersEqualArea(central_latitude=39.5, central_longitude=-98.35))
ax.scatter([p.x for p in cities_m.geometry], [p.y for p in cities_m.geometry], transform=ccrs.PlateCarree())
plt.show()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.