Matplotlib does not continue the line after NaN

I have an array slice:

array([1.0, 2.0, 3.0, None, 4.0, None, 5.0, None, 6.0, None], dtype=object) 

which when building looks like This

How to get dashed / connecting lines to continue after None values? I tried to change the data type from objects to floats using an astype, and None was replaced by nan, but it didn't matter, I also tried a masked array using np.where (np.isnan ()), etc., but it didn't make a difference. My build command is very simple:

 plt.plot(x, array, 'ro--') 

where x is a numpy array.

+3
source share
1 answer

This is the correct behavior, mpl tries to connect adjacent points. If you have points with nan on both sides, there is no proper way to connect them to anything.

If you want to just ignore your nan when plotting, then cross them out

 ind = ~np.isnan(np.asarray(data.astype(float))) plt.plot(np.asarray(x)[ind], np.asarray(data)[ind], 'ro--') 

All asarray must be sure that the example will work, the same with astype

it is also very bad to use variable names that are shadow (both in terms of python resolution and in terms of semantic) classes (like an array) or common functions.

+7
source

Source: https://habr.com/ru/post/1013370/


All Articles