Extracting map before plotting with matplotlib+cartopy

35 views
Skip to first unread message

Fruzsina Agocs

unread,
Sep 6, 2019, 10:19:14 AM9/6/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
def draw_img(dat):
   
""" Draw the selected area on a map of the Arctic, and plot the sea ice concentration over it. """
   plt
.figure()
   ax
= plt.subplot(projection=ccrs.NorthPolarStereo()) ax.set_extent([-180,180,60,90], ccrs.PlateCarree())
   img
= dat.isel(time=0).plot.pcolormesh('longitude', 'latitude',ax=ax, levels=range(0,    100,5),cmap='Blues_r',transform=ccrs.PlateCarree())
   arr
= img.get_array().reshape(img._meshHeight,img._meshWidth)
   plt
.show()
   
return arr

# Plot projected map
res
= draw_img(da1)
# Plot the extracted array
plt
.figure()
plt
.imshow(res)
plt
.show()


Hello,

I'm wondering if there's any way to capture an array matplotlib is going to plot just before plotting, that has been projected to distances from a grid over (latitude, longitude). Something like the code above, which produces the attached plots. This currently gives back the original array in latitude and longitude, even though it has been projected into 2D! The variable da1 stores an xarray.DataArray.

Thank you!

0BYqMU9 - Imgur.png

Klaus Zimmermann

unread,
Sep 6, 2019, 10:31:05 AM9/6/19
to scitoo...@googlegroups.com
Hi,

it is in the nature of this stuff that the data array does not change by
the projection. The array is not a map, it only says that the value
(here sea ice concentration) at indices (i, j) is x. But the location
information comes from a different source, in your xarray.DataArray from
the coordinate information.

So since the array does not change by the projection, what you get is
quite correct.

But the real question is: What are you actually trying to do?

Cheers
Klaus


On 06/09/2019 16:19, Fruzsina Agocs wrote:
> |
> defdraw_img(dat):
> --
> You received this message because you are subscribed to the Google
> Groups "SciTools (iris, cartopy, cf_units, etc.) -
> https://github.com/scitools" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scitools-iri...@googlegroups.com
> <mailto:scitools-iri...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/scitools-iris/55a1c5fb-147f-414b-89b1-3b30e8c03b8d%40googlegroups.com
> <https://groups.google.com/d/msgid/scitools-iris/55a1c5fb-147f-414b-89b1-3b30e8c03b8d%40googlegroups.com?utm_medium=email&utm_source=footer>.

Fruzsina Agocs

unread,
Sep 6, 2019, 11:09:47 AM9/6/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Hi,

Thank you for the clear and detailed explanation. What I'm actually trying to do is re-grid the array that's evenly spaced in (lat, lon) onto an array that's evenly spaced in (x, y), corresponding to a projection (preferably one that preserves area). I'd then retrieve this array as a numpy.array, which I'd then use to train a neural network. 

I've been suggested to use iris to do the re-gridding, so I'll try that first, but if you know another (short) solution, I'd be keen to learn.

Best,
Fruzsina

Klaus Zimmermann

unread,
Sep 6, 2019, 11:49:50 AM9/6/19
to scitoo...@googlegroups.com
Hi Fruzsina,

I think the advise you got is right. For this you will need to do
regridding. Unfortunately, I am not even sure whether iris' regridding
will work for you since the last time I tried it was not possible to
change the coordinate system completely and use 2d coordinates.

But maybe things have improved since then.

Good luck!
Klaus
> > an email to scitoo...@googlegroups.com <javascript:>
> > <mailto:scitools-iri...@googlegroups.com <javascript:>>.
> <https://groups.google.com/d/msgid/scitools-iris/55a1c5fb-147f-414b-89b1-3b30e8c03b8d%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/scitools-iris/55a1c5fb-147f-414b-89b1-3b30e8c03b8d%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "SciTools (iris, cartopy, cf_units, etc.) -
> https://github.com/scitools" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scitools-iri...@googlegroups.com
> <mailto:scitools-iri...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/scitools-iris/97570630-2c14-4a0a-a3bf-a9c8f9a0e7dc%40googlegroups.com
> <https://groups.google.com/d/msgid/scitools-iris/97570630-2c14-4a0a-a3bf-a9c8f9a0e7dc%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages