'How can I save Basemap (or, more in general, Matplotlib) contour output as an array?

I'm using Basemap package to plot some level curves on a projection map.

First of all I convert the longitudes and latitudes into local coordinates on the map.

local_x, local_y = m(lons, lats)

Then I plot plot some data on the map with the new coordinates

m.contour(local_x, local_y, data, 10, cmap='gray', vmin=0)

I remove the grid and axis

ax = fig.gca()
ax.axis('off')

And I get the following result

Output of the .contour function

How can I now save this picture as an array? By this I mean that I would like to be able to save to output as array in such a way that I can keep working with this image. For example being able to replot with plt.imshow(pic).

Apart from Basemap, the question is in general if the contour function of Matplotlib allows you to do that, since the contour function of Basemap calls inside the one of Matplotlib.

Comments:

  1. I know that one possibility is to save the pic from the canvas of matplotlib, but the issue with this is that the intensities of the contour lines are lost, and become instead pixel values between 0 and 255. What I want to do instead is to keep the original intensities.

  2. I know that there's the possibility to extract each path as a list of vertices and to have the list of all paths, but that's different from having all these values as an array, which is what I need.

  3. I also thought of saving the matplotlib result as a file and then to read it back, but, again, the values are in between 0 and 255.

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source