Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting points with no data to white with Matplotlib imshow

I am graphing data from a numpy array using matplotlib imshow. However, some points have no data in them. I initialized the array using np.zeroes, so these points are dragging down the whole map. I know that none of the data will ever have a value of 0.0. Is there some way for me to tell the imshow routine to ignore these points (ie leave them white so it is clear they are empty)?

like image 203
Elliot Avatar asked Apr 11 '12 21:04

Elliot


People also ask

How do I change my color on Imshow?

The most direct way is to just render your array to RGB using the colormap, and then change the pixels you want.

How do you normalize Imshow?

Just specify vmin=0, vmax=1 . By default, imshow normalizes the data to its min and max. You can control this with either the vmin and vmax arguments or with the norm argument (if you want a non-linear scaling).

What is interpolation in Imshow?

This example displays the difference between interpolation methods for imshow . If interpolation is None, it defaults to the rcParams["image. interpolation"] (default: 'antialiased' ). If the interpolation is 'none' , then no interpolation is performed for the Agg, ps and pdf backends.

How do I make Axis invisible in matplotlib?

How to hide axis in matplotlib figure? The matplotlib. pyplot. axis('off') command us used to hide the axis(both x-axis & y-axis) in the matplotlib figure.


1 Answers

Have you tried instantiating your array with NaNs instead of zeros to see if matplotlib's default will ignore the NaNs in a way that works for you? You could also try just using logical indexing to make the locations of 0 equal to NaN right before plotting:

my_data[my_data == 0.0] = numpy.nan

Alternatively, you can use the NaN idea and follow this link's advice and use NumPy masked arrays in order to plot the NaN entries as a color you prefer.

I think you could also use that link's idea to make a masked array at the zero locations too, without going to the NaN option if you don't like it.

like image 146
ely Avatar answered Oct 14 '22 22:10

ely