Take the 2-minute tour ×

This question already has an answer here:

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 plotted looks like THIS

How do I get the dotted/connecting lines to continue after the None values? I tried changing the data type from objects to floats using astype and the None's are replaced with nan's but it made no difference, I also tried a masked array using np.where(np.isnan()) etc. but it didn't make a difference. My plotting command is very simply:

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

where x is a numpy array.

share|improve this question

marked as duplicate by falsetru, tcaswell, Bakuriu, Adam Arold, RGraham Aug 26 '13 at 8:24


The "array" you show can not correspond to the axes you show. Is it an array of indices such as that from a numpy.where ? –  Jblasco Aug 25 '13 at 16:09
Jblasco - apologies, I changed the values so the array wasn't cumbersome to read. –  user1654183 Aug 25 '13 at 18:29

1 Answer 1

up vote 6 down vote accepted

This is the correct behavior, mpl tries to connect adjacent points. If you have points with a nan on either side, there is no valid way to connect them to anything.

If you want to just ignore your nans when plotting, then strip them out

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

All the asarray are just to be sure the example will work, same with the astype

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

share|improve this answer
tcaswell - thanks for your answer. In my code the array is not named array, I just changed it to that here so it was obvious what it was. It seems that changing the name and values of the array has caused more confusion than it solved :). Cheers again. –  user1654183 Aug 25 '13 at 18:30
