11

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

Elliot
  • 5,211
  • 10
  • 42
  • 70
  • 1
    You've got two good answers; consider selecting one as correct. I combined the two answer to accomplish my goal. They were both much easier than defining a cmap. – physicsmichael Feb 28 '14 at 05:49

2 Answers2

18

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.

Community
  • 1
  • 1
ely
  • 74,674
  • 34
  • 147
  • 228
3

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.

Devin Venable
  • 489
  • 3
  • 5