Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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}}]

smallPlot

bigPlotRescaled

Combined

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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]]}
share|improve this answer
    
Thanks Alexey, thats exactly the sort of trick I had hoped for but failed to find in the documentation. –  Jeremy Sonander Mar 23 at 19:40
1  
Later I added a legend and the code above broke. If you need to do the same the answer is here. –  Jeremy Sonander Mar 26 at 16:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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