Search code examples
pythondictionaryplotcartopy

My data won't plot over my python cartopy map


I am trying to plot some data over a map using cartopy in python. However, I am struggling to get it to show up over the map, any thoughts? Code is:

import pandas as pd
from matplotlib import pyplot as plt 
import cartopy.crs as ccrs
import cartopy
import cartopy.feature as cfeature

Results1 = Results.sort_values(by=["Lon", "Lat"])
xvals = Results1["Lon"].unique()
yvals = Results1["Lat"].unique()
zvals = Results1["WS Difference"].values.reshape(len(xvals), len(yvals)).T

plt.figure(figsize=(15,12))

central_lat = 37.5
central_lon = -96

extent = [xvals.min(), xvals.max(), yvals.min(), yvals.max()]
central_lon = np.mean(extent[:2])
central_lat = np.mean(extent[2:])

ax = plt.axes(projection=ccrs.AlbersEqualArea(central_lon, central_lat))
ax.set_extent(extent)
ax.coastlines()
ax.add_feature(cfeature.STATES, linewidth=1.5,edgecolor='black')

cp = ax.contourf(xvals, yvals, zvals,cmap=plt.cm.get_cmap('bwr', 22),levels= [-5,-4.5,-4,-3.5,-3,-2.5,-2,-1.5,-1,-0.5,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5],zorder=3,alpha=0.6)
cb=plt.colorbar(cp,orientation="horizontal",pad=0.1)
cb.set_label(label='Difference in long-term mean wind speed (%)',size='large')


plt.show()

enter image description here


Solution

  • The problem is likely related with your projection system. When plotting the windspeed data with ax.contourf you should specify the coordinate system of your data with transform = ccrs.YOURDATAPROJECTION. You can check here for a list of the projection systems supported by Cartopy.

    If this doesn't work then you should provide a sample of your data so that we can try to replicate your issue.