I am trying to combine two DateListPlots to produce a single chart with 2 y-axis. There is an example here with regular Plots and I have tried to adapt it for time based data https://reference.wolfram.com/mathematica/howto/GeneratePlotsWithTwoVerticalScales.html

The code below creates smallMeasurements and bigMeasurements, plots each on its own graph, rescales one so they can be seen on the same chart, and then combines them.

The problem I cannot fix is that on combining the charts the X-Axis no longer displays time. Does anyone know some magic to fix this?

timesSteps = Map[Function[AbsoluteTime[{2014, 1, 1, 9, #, 0}]], Range[0, 480, 10]];
values = 1 + N[Sin[2*Pi *Mod[timesSteps/60, 360]/360]];

smallMeasurements = ArrayReshape[Riffle[timesSteps, values], {Length[values], 2}];
bigMeasurements = ArrayReshape[Riffle[timesSteps, 1000*(2 - values)], {Length[values], 2}];

smallPlot = DateListPlot[smallMeasurements]
bigPlot = DateListPlot[bigMeasurements];

smallRange = ReplaceAll[PlotRange, AbsoluteOptions[smallPlot, PlotRange]][[2]];
bigRange = ReplaceAll[PlotRange, AbsoluteOptions[bigPlot, PlotRange]][[2]];

leftTicks = N[FindDivisions[smallRange, 5]];
rightTicks = Quiet[Transpose[{leftTicks, Map[Function[ToString[NumberForm[#, 2], StandardForm]], Rescale[leftTicks, smallRange, bigRange]]}]] ;

bigPlotRescaled = ReplaceAll[bigPlot, Graphics[graph_, s___] -> Graphics[GeometricTransformation[graph, RescalingTransform[{{0, 1}, bigRange}, {{0, 1}, smallRange}]], s]]

Show[smallPlot, bigPlotRescaled, Axes -> False, Frame -> True,
FrameStyle -> {ColorData[1] /@ {1, 2}, {Automatic, Automatic}},
FrameTicks -> {{leftTicks, rightTicks}, {Automatic, Automatic}}]





1 Answer 1


Your problem arises due to the following option in the final Show command:

FrameTicks -> {{leftTicks, rightTicks}, {Automatic, Automatic}}

Instead of {Automatic, Automatic} you should give explicit ticks specification taken from any of two DateListPlots (if they have identical ticks):

FrameTicks -> {{leftTicks, rightTicks}, Options[smallPlot, FrameTicks][[1, 2, 2]]}
  • $\begingroup$ Thanks Alexey, thats exactly the sort of trick I had hoped for but failed to find in the documentation. $\endgroup$ Mar 23, 2014 at 19:40
  • 1
    $\begingroup$ Later I added a legend and the code above broke. If you need to do the same the answer is here. $\endgroup$ Mar 26, 2014 at 16:29

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.