Logarithmic Plot with two y-axes in Mathematica

by Pascal Schulthess

Since there is no built-in Mathematica 6.0 function to generate a plot with two linear y-axes and a logarithmic x-axis, I found the following method which fulfilled my needs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
TwoAxisListLogPlot[f_List, g_List, frange_, grange_, color1_, color2_,
   opts___?OptionQ] := 
 Module[{old, new, scale, fm, fM, gm, gM, newg}, {fm, fM} = frange;
  {gm, gM} = grange;
  scale[var_] := ((var - gm) (fM - fm))/(gM - gm) + fm;
  old = AbsoluteOptions[
     ListPlot[g, Frame -> True, PlotRange -> grange, 
      DisplayFunction -> Identity], FrameTicks][[1, 2, 2]];
  new = (Prepend[Rest[#1], scale[First[#1]]] &) /@ old;
  newg = 
   Transpose[{Transpose[g][[1]], 
     Map[scale, Transpose[g][[2]], {1, 2}]}];
  ListLogLinearPlot[{f, newg}, Frame -> True, 
   FrameTicks -> {Automatic, Automatic, None, new}, 
   PlotStyle -> {{color1}, {color2}}, 
   FrameStyle -> {{}, {color1}, {}, {color2}}, 
   PlotRange -> frange*(1 + .00 (fM - fm)), opts]]

This defines the new function TwoAxisListLogPlot. The usage of this new function is:

18
19
20
21
22
23
24
25
26
Labeled[Labeled[
  TwoAxisListPlot[data1, data2, {5, 8.3}, {450, 460}, Red, Blue, 
   Axes -> False, Joined -> {True, False}, 
   PlotMarkers -> {Style[\[EmptyCircle], Red, Medium, 
      Background -> White], 
     Style[\[FilledUpTriangle], Blue, Medium]}], {Style["X", 
    FontFamily -> Times], 
   Style["Y", Red, FontFamily -> Times]}, {Bottom, Left}], 
 Style["Y-alt", Blue, FontFamily -> Times], Right]

where data1 and data2 represent the two datasets, obviously. The resulting plot looks like this:
ListLogLinearPlot with two y-axes

It is also possible to do a double logarithmic plot by changing ListLogLinearPlot in line 13 into ListLogLogPlot.
A running Mathematica example can be found in the repository.