3

Why does matplotlib/numpy not plot unmasked points with a line? This issue is only with plotting as a line. Scatter or point plotting works okay.

import numpy as np
import matplotlib.pyplot as plt

x = np.ma.array([0, 1, 2, 3, 4])

x[[1, 3]] = np.ma.masked # issue is only when its set this way; but this is recommended in the docs
# x = np.ma.masked_where(x < 3) # this works fine for all plotting

plt.plot(x) # this shows nothing, but the axes are set correctly
plt.show()

plt.plot(x, "o") # this shows the points
plt.show()
Tyler Estes
  • 59
  • 1
  • 5
  • https://stackoverflow.com/questions/6392719/python-scatter-plot-with-numpy-masked-arrays make sure that all of is np.array – PV8 Jun 26 '19 at 06:20
  • I appreciate you just googling for the answer. But, I already did that previously and found the same post. You'll notice it's not relevant to my question for a few reasons. 1) The method of masking. I explicitly state that using the np.ma.masked_* methods work fine for plotting. I'm not asking about those. 2) I explicitly mention scatter plots are not the issue which the post you link to only deals with scatter plots. – Tyler Estes Jun 26 '19 at 09:26
  • Please run my code and examine the different cases before responding. I have already googled and I have already tested multiple cases. I'm more interested in why, or if there is a workaround to **keep a masked array** for plotting line plots using the notation that is actually recommended in the [np.ma module notes](https://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html) – Tyler Estes Jun 26 '19 at 09:30
  • 1
    I suppose the problem is too trivial to be found somewhere. If you mask every second point, one of the two points of each line segment is masked. Since you cannot draw a line between a point and no point, no lines are drawn at all. – ImportanceOfBeingErnest Jun 26 '19 at 13:01
  • Ahh, so it was actually behaving as I expected my test was just flawed. Ooof. I didn't even pay attention to the fact that I was deleting all the points that would connect. That's a poor example on my part. For anyone else does come across this, the issue was with the points I chose to delete in my example. Matplotlib plots masked points exactly the same as it plots np.nan points noted [here](https://matplotlib.org/3.1.0/gallery/lines_bars_and_markers/nan_test.html) – Tyler Estes Jun 28 '19 at 09:23

0 Answers0