I have a set of {x,y} data that shows a measurement value over time on the x axis. I'd like to highlight several chunks of time on my ListLinePlot by filling the plot to the axis for only certain x-ranges (say, 10-20 and 55-96).

I've tried the methods outlined here: ListLinePlot partial filling

data1 = movavgjoined; data1[[;; 9]] = Null; data1[[21 ;;]] = Null;
ListLinePlot[{movavgjoined, data1}, Filling -> {2 -> Axis}]

But the output is just the original graph.

Edit: here's some sample data

movavgjoined = {{0.00416667,5.50648},{0.00833333,5.51119},{0.0125,5.51531}, 

1 Answer 1

yintervals = {{5.55, 5.58}, {5.61, 5.66}};
data2 = If[IntervalMemberQ[IntervalUnion @@ (Interval /@ yintervals), #[[2]]], 
 #, {#[[1]], Null}] & /@ movavgjoined;

ListLinePlot[{movavgjoined, data2}, Filling -> {2 -> Axis}, PlotStyle -> ColorData[97][1]]

enter image description here

xintervals = {{.05, .08}, {.11, .13}};
data3 = If[IntervalMemberQ[IntervalUnion @@ (Interval /@ xintervals), #[[1]]], 
    #, {#[[1]], Null}] & /@ movavgjoined;

ListLinePlot[{movavgjoined, data3}, Filling -> {2 -> Axis}, PlotStyle -> ColorData[97][1]]

enter image description here

  • $\begingroup$ Thank you for your suggestion! Okay so this is working but it's filling the wrong part of my plot. I think this is because these functions call the nth list element rather than as a range of y values. My y values are time values in minutes/fractions of minutes and are irregularly spaced. So the 10th-20th time point is being filled as opposed to 10 minutes-20 minutes. $\endgroup$
    – user63997
    May 6, 2019 at 4:34
  • $\begingroup$ @user63997, it would be easier to suggest a solution if you can post your movavgjoined. $\endgroup$
    – kglr
    May 6, 2019 at 4:37
  • $\begingroup$ True! I edited the original post with some sample data. It's a big list, so I just included the first few points so you get the idea. $\endgroup$
    – user63997
    May 6, 2019 at 4:53
  • $\begingroup$ Shoot, I see now that I made a mistake in my original post. The X axis is the time points, not the Y. So the above code uses a y-range which you've defined. The graph is exactly what I'm wanting, though. So say here instead of defining the y values, the time ranges on the x axis that I'd like to fill are {0.5, 0.8} and {0.11,0.13}. Would the code be the same? $\endgroup$
    – user63997
    May 6, 2019 at 7:19
  • $\begingroup$ @user63997, updated with the version that uses intervals of x-coordinates. $\endgroup$
    – kglr
    May 6, 2019 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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