 Hi,

I have a 2D masked array, created like:

import numpy as np
data = np.ma.array(data, mask=[data == 'NA'])

which I would like to plot as a heatmap.

import pylab

pylab.pcolor(data)
or
pylab.pcolormesh(data)

Well, it works with any array, but not if masked values are in there.
Can somebody supply me with a snippet, as I apparently don't get the
relevant piece in the docs (or did not find it ).

TIA
Christian

Christian Meesters wrote:

Hi,

I have a 2D masked array, created like:

import numpy as np
data = np.ma.array(data, mask=[data == 'NA'])

which I would like to plot as a heatmap.

import pylab

pylab.pcolor(data)
or
pylab.pcolormesh(data)

Well, it works with any array, but not if masked values are in there.
Can somebody supply me with a snippet, as I apparently don't get the
relevant piece in the docs (or did not find it ).

TIA
Christian

Christian: That should work, if you created the masked array correctly. Why are you creating the mask with data=='NA'? I suspect that this always evaluates to False, so you don't get a mask. You probably want to check for a numeric value, not a string. For example:

import matplotlib.pyplot as plt
import numpy as np
def func3(x,y):
return (1- x/2 + x**5 + y**3)*np.exp(-x**2-y**2)
dx, dy = 0.05, 0.05
x = np.arange(-3.0, 3.0001, dx)
y = np.arange(-3.0, 3.0001, dy)
X,Y = np.meshgrid(x, y)
Z = func3(X, Y)
plt.pcolor(Z)
plt.show()

-Jeff

Christian: That should work, if you created the masked array
correctly. Why are you creating the mask with data=='NA'? I suspect
that this always evaluates to False, so you don't get a mask. You
probably want to check for a numeric value, not a string. For example:

Thanks a lot, Jeff!

Yes, the non-numerical comparison was indeed causing problems - although
I don't understand why.

However, I can easily inject numerical non-sense values into the array.
As to the 'NA': The data are an R output file. As I don't like R too
much, I'm falling back to Python.

Christian

Christian Meesters wrote:

Christian: That should work, if you created the masked array correctly. Why are you creating the mask with data=='NA'? I suspect that this always evaluates to False, so you don't get a mask. You probably want to check for a numeric value, not a string. For example:

Thanks a lot, Jeff!

Yes, the non-numerical comparison was indeed causing problems - although
I don't understand why.

However, I can easily inject numerical non-sense values into the array.
As to the 'NA': The data are an R output file. As I don't like R too
much, I'm falling back to Python.

Christian

Christian: What type of array is that (data.dtype)? I don't see how a numpy array can have values equal to 'NA', unless it is an array of strings. In that case, it would not be plottable anyway.

-Jeff

Jeff, that's a good point. I remember ...

···

On Thu, 2009-08-27 at 07:54 -0600, Jeff Whitaker wrote:

Christian Meesters wrote:
>> Christian: That should work, if you created the masked array
>> correctly. Why are you creating the mask with data=='NA'? I suspect
>> that this always evaluates to False, so you don't get a mask. You
>> probably want to check for a numeric value, not a string. For example:
>>
> Thanks a lot, Jeff!
>
> Yes, the non-numerical comparison was indeed causing problems - although
> I don't understand why.
>
> However, I can easily inject numerical non-sense values into the array.
> As to the 'NA': The data are an R output file. As I don't like R too
> much, I'm falling back to Python.
>
> Christian
>
>
Christian: What type of array is that (data.dtype)? I don't see how a
numpy array can have values equal to 'NA', unless it is an array of
strings. In that case, it would not be plottable anyway.

-Jeff