I have a list of data to plot. I would like to plot them with lines but delete some data before plotting and leave the lines disconnected where data is deleted. For example,

ListPlot[Sin[0.5 Range@100] /. {a_?Negative -> Missing[]}, Joined -> True]

enter image description here

This works well. However, the data is not dense enough to make smooth plots. Thus I want to turn on interpolation:

ListPlot[Sin[0.5 Range@100] /. {a_?Negative -> Missing[]}, 
 Joined -> True, InterpolationOrder -> 2]

However, This ends up with en error message and the same figure without interpolation.

ListPlot::ioproc: "{{1.,0.479426},{2.,0.841471},{3.,0.997495},{4.,0.909297},{5.,0.598472},{6.,0.14112},<<39>>,{46.,Missing[]},{47.,Missing[]},{48.,Missing[]},{49.,Missing[]},{50.,Missing[]},<<50>>} may contain non-machine-precision numbers, complex numbers, or invalid entries."

I think to set the data points to Missing[] should be the right way to remove data. Thus is it a bug that the data fail to interpolate?

I would have first interpolated data by hand using

Interpolation[data] /@ 
 Range[Min[data[[All, 1]]], Max[data[[All, 1]]], 
  (Max[data[[All, 1]]] - Min[data[[All, 1]]] + 1)/(10.*Length[data])]

and send the result to ListPlot. But let me still ask this question because I thought InterpolationOrder -> 2 should work but it didn't.

  • $\begingroup$ Have you tried Null instead of Missing[]? I'm not near a computer with Mathematica to check, but this works for me in other contexts. But you will probably still need to index the data. $\endgroup$ – Verbeia Apr 11 '14 at 15:13
  • $\begingroup$ @Verbeia : Thanks for your comment. Unfortunately Null does not work either (it works in that it removes the data and leaves lines disconnected. But it does not work with InterpolationOrder -> 2). $\endgroup$ – Yi Wang Apr 11 '14 at 15:17

I can't think of a way to do this without indexing the data. If that's not a problem, the following will do:

  indexed = MapIndexed[
    {Sequence @@ #2, #1} &,
    Sin[0.5 Range@100] /. {a_?Negative -> Missing[]}
   NumericQ@Last@# &
  Joined -> True,
  InterpolationOrder -> 2
(* Some errors spat out in trying to interpolate a series of Missing bits*)

enter image description here

  • $\begingroup$ Thanks for the answer! My work around currently is to first interpolate data. Your answer provides another interesting work around :) $\endgroup$ – Yi Wang Apr 11 '14 at 11:13
tD = TemporalData[Sin[0.5 Range@100] /. {a_?Negative -> Missing[]}, {1}];

ListPlot[DeleteCases[Split[tD["Paths"][[1]], #[[2]] != #2[[2]] &], {{_, _}}], 
 Joined -> True, InterpolationOrder -> 2]

enter image description here


You could just do without Missing:

ListPlot[Cases[{#, Sin[0.5 #]} & /@ Range[100], {_, _?NonNegative}], 
 Joined -> True]

enter image description here

  • $\begingroup$ Thanks a lot for the answer! However, this is exactly what I would tried to avoid. I want to break the lines where the data have been deleted. And my problem arises because of this requirement. $\endgroup$ – Yi Wang Apr 11 '14 at 9:35
  • $\begingroup$ @YiWang ok. Sorry $\endgroup$ – ubpdqn Apr 11 '14 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.