2

I want to combine a TimeLinePlot with a DateListPlot. My questions are:

  1. Can you change the Y-axes of a TimeLinePlot?
  2. Can you visualise the Y-axes of the DatelistPlot if you combine both plots using the Show function?

I use the next datasets:

data1e = {{DateObject[{2017, 01, 01}], 
     "event1"}, {DateObject[{2017, 01, 02}], 
     "event2"}, {DateObject[{2017, 01, 03}], 
     "event3"}, {DateObject[{2017, 01, 04}], 
     "event4"}, {DateObject[{2017, 01, 05}], 
     "event5"}, {DateObject[{2017, 01, 06}], 
     "event6"}, {DateObject[{2017, 01, 07}], "event7"}};
 data1d = {{DateObject[{2017, 01, 01}], 
    1}, {DateObject[{2017, 01, 02}], 2}, {DateObject[{2017, 01, 03}], 
    3}, {DateObject[{2017, 01, 04}], 4}, DateObject[{2017, 01, 05}], 
   5}, {DateObject[{2017, 01, 06}], 6}, {DateObject[{2017, 01, 07}], 
  7}}; data2e = {{DateObject[{2017, 01, 08}], 
   "event1"}, {DateObject[{2017, 01, 09}], 
   "event2"}, {DateObject[{2017, 01, 10}], 
   "event3"}, {DateObject[{2017, 01, 10}], 
   "event4"}, {DateObject[{2017, 01, 10}], 
   "event5"}, {DateObject[{2017, 01, 10}], 
   "event6"}, {DateObject[{2017, 01, 10}], 
   "event7"}, {DateObject[{2017, 01, 11}], 
   "event8"}, {DateObject[{2017, 01, 12}], 
   "event9"}, {DateObject[{2017, 01, 13}], 
   "event10"}, {DateObject[{2017, 01, 14}], "event11"}};
data2d = {{DateObject[{2017, 01, 08}], 
    110}, {DateObject[{2017, 01, 09}], 
    120}, {DateObject[{2017, 01, 10}], 
    130}, {DateObject[{2017, 01, 11}], 
    140}, {DateObject[{2017, 01, 12}], 
    150}, {DateObject[{2017, 01, 13}], 
    160}, {DateObject[{2017, 01, 14}], 170}};

I write 4 scripts to generate the different graphs.

TLP1 = TimelinePlot[
  MapThread[Labeled, {data1e[[All, 1]], data1e[[All, 2]]}]
  , PlotTheme -> "Detailed"
  , ImageSize -> 500
  , PlotRange -> {{DateObject[{2017, 01, 01}], 
     DateObject[{2017, 01, 14}]}, Automatic}];

DLP1 = DateListPlot[data1d
  , PlotTheme -> "Detailed"
  , ImageSize -> 500
  , PlotRange -> {{DateObject[{2017, 01, 01}], 
     DateObject[{2017, 01, 14}]}, {0, 200}}];

Show[{TLP1, DLP1}]

enter image description here

TLP2 = TimelinePlot[
  MapThread[Labeled, {data2e[[All, 1]], data2e[[All, 2]]}]
  , PlotTheme -> "Detailed"
  , ImageSize -> 500
  , PlotRange -> {{DateObject[{2017, 01, 01}], 
     DateObject[{2017, 01, 14}]}, {0, 200}}];

DLP2 = DateListPlot[data2d,
  , PlotTheme -> "Detailed"
  , ImageSize -> 500
  , PlotRange -> {{DateObject[{2017, 01, 01}], 
     DateObject[{2017, 01, 14}]}, {0, 200}}];

Show[{TLP2, DLP2}]

enter image description here

Both graphics has different sizes. Becasue I want to combine them in a (powerpoint) presentation I would like to have them the same sizes. And I want to show the the X-axes and the Y-axes.

Anyone a suggestion how to solve this issue?

Michiel van Mens
  • 2,835
  • 12
  • 23

1 Answers1

2

Update: For versions 11.3 and 12.2 we can use the following two functions to post-process the TimeLinePlot output to modify the vertical scale:

$Version
"11.3.0 for Microsoft Windows (64-bit) (March 7, 2018)"
ClearAll[modifyScaleV11]
modifyScaleV11[g1_, g2_] := MapAt[GeometricTransformation[#, 
    RescalingTransform @@ (PlotRange /@ {g1, g2})] &, g1, {1}]

Show[DLP2, modifyScaleV11[TLP2, DLP2]]

enter image description here

$Version (* Wolfram Cloud *)
"12.2.0 for Linux x86 (64-bit) (November 16, 2020)" 
ClearAll[modifyScaleV12]

modifyScaleV12[g1_, g2_] := Module[{pr = {PlotRange[g1][[1]], {0, 1 + Max@Cases[g1, Inset[, Offset[, {, a}], ___] :> a, All]}}}, MapAt[GeometricTransformation[#, RescalingTransform[pr, PlotRange[g2]]] &, g1, {1}]]

Show[DLP2, modifyScaleV12[TLP2, DLP2]]

enter image description here

Original answer:

You can modify the output of TimeLinePlot by rescaling the y-coordinates of the graphics primitives in it.

The function scaleF[g1, g2] below creates a scaling function that rescales any number in the vertical PlotRange of g1 to the vertical PlotRange of g2. The function modifyF[g1, g2] replaces the graphics primitives in g1 with their rescaled versions.

ClearAll[scaleF, modifyF]
scaleF[g1_, g2_] := Rescale[#, ## & @@ (PlotRange[#][[2]] & /@ {g1, g2})] &;

modifyF[g1_, g2_] := g1 /. {(head : Alternatives[Polygon, Point, Line])[x__] :> head[x /. {Offset[y_, {a_, b_}] :> Offset[y, {a, scaleF[g1, g2]@b}], {c_, d_} :> {c, scaleF[g1, g2]@d}} ], Inset[ins_, pos_, rest___] :> Inset[ins, pos /. Offset[y_, {a_, b_}] :> Offset[y, {a, scaleF[g1, g2]@b}], rest ]};

Example:

Show[DLP2, modifyF[TLP2, DLP2]]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Your answers are always very useful. I recently made a package that utilizes one of your MSE answers for making heat-map plots. – Anton Antonov Oct 13 '17 at 14:01
  • Thank you @Anton. Playing with your package now. – kglr Oct 13 '17 at 14:30
  • I have to say it does not always work as expected. I have to re-write the MatrixPlot parts. – Anton Antonov Oct 13 '17 at 17:04
  • Hi @kglr, did youn changed something in your answare?. When I use your code I get not the same result. – Michiel van Mens Oct 18 '17 at 12:11
  • @MichielvanMens, I have not made any changes in the code. Please note that, in version 11, forPlotRange[g]to work explicit values need to be set ingfor the optionPlotRange`. – kglr Oct 18 '17 at 14:33
  • Hi @kglr The first time I used your code, it works fine. Until yesterday, When I tried your code again, the code didn't give the same result. So I tried your code in de Wolfram-develoment environment and it's works fine. I don't understand this. Do you? – Michiel van Mens Oct 18 '17 at 20:36
  • @Michiel, I am using wolfram-development environment; i don't have acces to desktop v11. – kglr Oct 18 '17 at 20:56
  • @kglr, Until MMA 12.0 your solutions worked fine. Now I upgraded to 12.2 and wanted to re-use your code. In version 12.2 it doesn't work any more. Do you know why? My question is because i do not understand your code but I want to use it. – Michiel van Mens Feb 16 '21 at 08:11
  • 1
    @MichielvanMens, please see the update. – kglr Feb 16 '21 at 09:29