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)?

4/11/2012 9:59:20 PM

Accepted Answer

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.

5/23/2017 11:47:09 AM

Pad the array with Python None for points that should not display.

y_series_1 = [1,None,None,4,5]
y_series_2 = [1,2,5,6,7]

For this example, the y_series_1 line will disappear from the X axis at the second and third point. The result is line breaks (line begins, disappears, then continues at the fourth point), which I believe is the behavior you are after.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow