Cartopy central_longitude causes lat/lon co-ordinates to be plot in incorrect place

Viewed 50

I am plotting a flight track with lat/lon co-ordinates. The following code produces an accurate lat/lon plot, but the flight path is cut off.

fig = plt.figure(fig size(16,8))
ax = plot.subplot(1,1,1, projection = cars.PlateCarree())

ax.coastlines()
#Add gridlines and customise
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth = 2)
gl.xlabels_top = False
gl.ylabels_left = False
ax.add_feature(cfeature.OCEAN)
ax.add_feature(feature.LAND)
ax.set_ylim(-90, 60)

#Plot lon/lat points as scatter points 
im.ax.scatter(lon, lat, s = 20)

This shows an image of the successful code without central_longitude applied - this is the correct flight path track: This shows an image of the successful code without central_longitude applied - this is the correct flight path track

I would like for the flight track to be in the middle of the plot - not cut off at the edges of the plot. I insert "central_longitude" when defining the projection as follows:

fig = plt.figure(fig size(16,8))
ax = plot.subplot(1,1,1, projection = cars.PlateCarree(central_longitude = 40))

ax.coastlines()
#Add gridlines and customise
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth = 2)
gl.xlabels_top = False
gl.ylabels_left = False
ax.add_feature(cfeature.OCEAN)
ax.add_feature(feature.LAND)
ax.set_ylim(-90, 60)

#Plot lon/lat points as scatter points 
im.ax.scatter(lon, lat, s = 20)

This results in the flight path in an incorrect location, shown in the next figure. The map has moved accordingly, but the flight path has remained in place. How should I fix this?

Flight path with "central_longitude = 40" applied - the map has moved but the flight track is still in the same place:

Flight path with "central_longitude = 40" applied - the map has moved but the flight track is still in the same place.

1 Answers

Since your axes have a different projection from your data, you need to tell cartopy to transform the data from a specific projection:

im.ax.scatter(lon, lat, s=20, transform=ccrs.PlateCarree())

See the cartopy docs on understanding the projection and transform keywords for more info

Related