possible kludge for masked value colors in contourf?

As far as I can tell, you cannot specify the color that contourf uses for masked values.
The examples enclosed with matplotlib ( 0.98.3) specifically comment that this is the case.
It appears that contourf just does not plot anything where the masked values occur and the background ( usually white ) is what is seen.
This is very useful but ....

I would like to have a bit more choice in how the areas of masked data are represented.
I came up with the following hack which seems to work, but being far from expert I wanted to know if anyone wished to help or give advice.
Is this reasonable or what ?

In the code below data is a masked array with some areas masked.
The result is that the missing areas are now black rather than white ( the default background, I guess)

fig = pylab.figure()
ax = fig.add_subplot(111)
ax.axesPatch.set_facecolor('k')
cs = ax.contourf(data)
pylab.savefig('example')

I am aware that pcolor , pcolormesh and using cm.set_bad will work but I also wanted the option for contourf - it produces nice looking plots.
Thanks for any help.

--Jim

James Boyle wrote:

As far as I can tell, you cannot specify the color that contourf uses for masked values.
The examples enclosed with matplotlib ( 0.98.3) specifically comment that this is the case.
It appears that contourf just does not plot anything where the masked values occur and the background ( usually white ) is what is seen.
This is very useful but ....

I would like to have a bit more choice in how the areas of masked data are represented.
I came up with the following hack which seems to work, but being far from expert I wanted to know if anyone wished to help or give advice.
Is this reasonable or what ?

Jim,

It strikes me as the right way to do what you want; it is not a kludge or a hack at all. Contourf generates patches for unmasked regions, not for the masked region, so it is indeed up to you to set the background as you have done.

I see that there is also an Axes.set_axis_bgcolor method, which I would expect to be preferred; but it is strangely named. Should it not be just Axes.set_bg or Axes.set_bgcolor? It corresponds to the axisbg kwarg in the Axes initializer, perhaps another unfortunate choice of names.

(Contourf has a longstanding bug in handling internal masked regions; I hope you don't run into it. Both Mike D and I have tried to figure out how to fix that bug, but so far without success.)

In the code below data is a masked array with some areas masked.
The result is that the missing areas are now black rather than white ( the default background, I guess)

fig = pylab.figure()
ax = fig.add_subplot(111)
ax.axesPatch.set_facecolor('k')
cs = ax.contourf(data)
pylab.savefig('example')

I am aware that pcolor , pcolormesh and using cm.set_bad will work but I also wanted the option for contourf - it produces nice looking plots.

I have not checked just now, but as I recall, pcolor acts like contourf with respect to masked regions--it plots nothing there, leaving the background showing through.

Eric