Plotting masked arrays with plot_surface() #487

Open
AndreasMatthias opened this Issue Sep 22, 2011 · 9 comments

Comments

Projects
None yet
5 participants

I would be nice if plot_surface() could plot masked arrays. Unlike imshow() which really plots the masked data,
plot_surface() only plots the non-masked data.
Here is an example:

import numpy as np
import pylab as mpl
from mpl_toolkits.mplot3d import axes3d

x = np.arange(1,10,1)
y = np.arange(1,10,1)
x,y = np.meshgrid(x,y)
z = x**3 + y**3 - 500
z = np.ma.masked_array(z, z<0)

cm = mpl.cm.jet

ax1 = mpl.subplot(1,2,1, projection='3d')
ax1.plot_surface(x,y,z,
                 rstride=1, cstride=1, linewidth=0,
                 cmap=cm)

ax2 = mpl.subplot(1,2,2)
ax2.imshow(z, cmap=cm)

mpl.show()

WeatherGod was assigned Sep 22, 2011

Member

dmcdougall commented Feb 29, 2012

I hacked together a very basic solution.

Would something like this be suitable?

Yes, that's what I was looking for. Great.

Member

dmcdougall commented Feb 29, 2012

And how should the colourmap behave in this case? Normalised to the min/max of the unmasked values?

Damon McDougall wrote:

And how should the colourmap behave in this case? Normalised to the
min/max of the unmasked values?

I'm plotting measurement data and want to mask all values which
are probabely measurement errors. Thus, normalising the colourmap
to the unmasked values would be correct for my use case.

Member

WeatherGod commented Feb 29, 2012

That would be incorrect in the general case. Often, masked values may be nans or unitialized garbage. As a general rule in mpl, you don't unmask input data.

Member

WeatherGod commented Feb 29, 2012

Of course, we probably should clarify terminology here...

"unmasked" as a verb means to take a masked array and remove its mask
"unmasked" as a noun means the elements of the masked array where the mask is False.

So, rereading @AndreasMatthias statement, I think you meant the noun. (whereas I was using the verb)

Member

dmcdougall commented Feb 29, 2012

@WeatherGod, I was just about to clarify what I meant but I see you got there before me. I meant the noun. I should normalise the colourmap using only the values whose mask evaluates to False. This is exactly what @AndreasMatthias expects as default behaviour.

This will require a little more work, since all I did in that picture was to simply not add the polygons to the Poly3DCollection if their mask was True.

Would there be wider interest for me to continue my efforts for this cause?

Member

WeatherGod commented Feb 29, 2012

Oh, absolutely. I would certainly welcome any sort of assistance here. Do note that I am about to embark on a refactoring of the plot_surface code with regards to documentation, shading, facecolors and such. We probably should coordinate so that we don't start stepping on each others' toes here.

tacaswell added this to the unassigned milestone Aug 18, 2014

temmeand commented Jul 3, 2015

Any progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment