0

I have a masked array in numpy:

masked = np.ma.core.MaskedArray([1, 2], mask=[True, False])

Now I want to display the numbers in a plot, so I need formatting:

"{:.2f}".format(masked[0])
'0.00'
"{:.2f}".format(masked[1])
'2.00'

Why is a masked element formatted as '0.00' and how can I avoid this, e.g. by formatting the masked element to NaN or some arbitrary character?

user3017048
  • 2,711
  • 3
  • 22
  • 32
  • But why would you have to format it? You can display masked data in a `matplotlib` plot simply with `plt.plot(x,masked)`. – Ardweaden Jul 31 '19 at 10:02
  • Use `masked.filled(fill_value)` to replace the masked value(s) with the desired fill value. Beware, though, that you have an integer dtype array, so you can only fill it with integers, not strings or `nan` (a float). – hpaulj Jul 31 '19 at 15:05
  • Oh, perfect, @hpaulj that did the job! You may post thi as an answer if you like! @ Ardweaden: It was a bit more complicated, I did an annotated heat map with plt.imshow, and the text is actually coming from that array I introduced in the question. – user3017048 Jul 31 '19 at 20:20

0 Answers0