3
$\begingroup$

I have been trying to figure out how to shade between Gridlines and I couldn't on my own, but found this post that almost has the perfect response except that the spaces between the frame and the first/last points are not shaded, like you can see on the image.

enter image description here

Tried editing the code presented in the webpage but they all led to errors or the shaded box not being visible anymore.

Could anyone indicate a way of accomplishing a full shading between gridlines? Be it from this code or in any other way?

Thank you!

data={{"Mon 23 Jan 2017 12:41 PM",152},{"Mon 23 Jan 2017 12:44 PM",146},{"Mon 23 Jan 2017 6:11 PM",143}, {"Mon 23 Jan 2017 6:14 PM",142},{"Tue 24 Jan 2017 5:47 PM",135},{"Tue 24 Jan 2017 5:51 PM",126}, {"Wed 25 Jan 2017 6:22 PM",129},{"Wed 25 Jan 2017 6:22 PM",109},{"Thu 26 Jan 2017 12:47 PM",137}, {"Thu 26 Jan 2017 12:49 PM",132},{"Thu 26 Jan 2017 6:36 PM",141}};
thePlot = DateListPlot[data, Joined -> True, PlotRange -> {Automatic, {80, 180}}, 
   GridLines -> {None, {60, 85, 100, 140}}, GridLinesStyle -> Directive[Gray, Thin], ImageSize -> 540];
gr = Graphics[{Opacity[.1], Blue, Rectangle @@ Transpose[{First[PlotRange /. AbsoluteOptions[thePlot, PlotRange]], {100, 140}}]}];

Show[thePlot, gr]

PS: I am copying the snippet of code from the post because I am importing the data from a .csv file.

$\endgroup$
  • 2
    $\begingroup$ try Show[thePlot, gr, PlotRangePadding -> 0]? $\endgroup$ – kglr Sep 12 at 19:12
2
$\begingroup$

You may use Epilog with DateObject locations on the date-axis in DateListPlot.

Therefore you can

  1. get the DateBounds on the date-axis,
  2. expand this date range to cover the padded plot range,
  3. plot the Rectangle.

The expanded date rage by 1 day on both ends can be obtained with DatePlus and Inner by

Inner[DatePlus, DateObject /@ DateBounds@data[[All, 1]], {-1, 1}, List]

Mathematica graphics

Then the above in DateListPlot's Epilog with Rectangle

DateListPlot[data,
 Joined -> True,
 PlotRange -> {Automatic, {80, 180}},
 GridLines -> {None, {60, 85, 100, 140}},
 GridLinesStyle -> Directive[Gray, Thin],
 ImageSize -> 540,
 Epilog -> {Opacity[.1, Blue],
   Rectangle @@
    Transpose@{
      Inner[DatePlus, DateObject /@ DateBounds@data[[All, 1]], {-1, 1}, List],
      {100, 140}
      }
   }
 ]

Mathematica graphics

Note the you can also use Prolog if you want the shaded area plotted first.

Hope this helps.

$\endgroup$
2
$\begingroup$

Scaled[] along with AbsoluteTime[] is usable in Rectangle[], which allows us to produce the following plot:

DateListPlot[data, GridLines -> {None, {60, 85, 100, 140}},
             GridLinesStyle -> Directive[Gray, Thin], Joined -> True, 
             PlotRange -> {Automatic, {80, 180}}, 
             Prolog -> {Opacity[0.2, Blue], 
                        Rectangle[Scaled[{-1, 0}, {AbsoluteTime[data[[1, 1]]], 100}], 
                                  Scaled[{1, 0}, {AbsoluteTime[data[[-1, 1]]], 140}]]}]

plot with shaded area

$\endgroup$
2
$\begingroup$

You can use Charting`get2DPlotRange[thePlot to get the plot range taking into account plot range paddings and use it to specify the rectangle coordinates:

gr = Graphics[{Opacity[.1], Blue, 
 Rectangle @@ Transpose[{Charting`get2DPlotRange[thePlot][[1]], {100,  140}}]}];

Show[thePlot, gr]

enter image description here

$\endgroup$

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.