Using Longitude and Latitude to Plot in Xarray

Using Longitude and Latitude to Plot in Xarray

I am trying to make seasonal maps out of climate datasets using the function imshow from the matplotlib toolbox. My problem is that I can only get imshow to plot the logical coordinates x and y which makes my map of Spain upside down: I have tried different methods and I think one of my problems might be that my longitude and latitude coordinates are 2d data arrays and not just vectors.

So far my code looks like this:

    seasonal = PET.groupby('time.season').mean(dim=['time'])
    seasonal.plot.imshow(col= 'season', robust = True)

And my data looks like this:

<xarray.DataArray 'PET' (year: 1, x: 834, y: 1115)>
array([[[nan, nan, nan, ..., nan, nan, nan],
        [nan, nan, nan, ..., nan, nan, nan],
        [nan, nan, nan, ..., nan, nan, nan],
        ...,
        [nan, nan, nan, ..., nan, nan, nan],
        [nan, nan, nan, ..., nan, nan, nan],
        [nan, nan, nan, ..., nan, nan, nan]]], dtype=float32)
Coordinates:
    lon      (x, y) float64 ...
    lat      (x, y) float64 43.99 43.99 43.99 43.99 ... 35.75 35.75 35.75 35.75
  * year     (year) int64 2008
Dimensions without coordinates: x, y

Does anyone know how to solve this, so imshow plots with longitude and latitude instead of x and y?

4

1 Answer

You've found from comments on the question that theimshow arg, origin='upper', will display your plots in the correct orientation:

seasonal.plot.imshow(col= 'season', robust = True, origin='upper')

To plot your axes in geographic lon, lat vs. logical x, y, trying using the imshow arg, extent=[left, right, bottom, top], to set the bounds of the axes and plot in these coordinates. Something like this:

seasonal.plot.imshow(col= 'season', robust = True, origin='upper', extent=[min(lon), max(lon), min(lat), max(lat)])

You can read more about origin and extent arg's inimshow here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.