MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: second y-axis in a graph

  • To: mathgroup at
  • Subject: [mg20566] Re: [mg20515] second y-axis in a graph
  • From: "Wolf, Hartmut" <hwolf at>
  • Date: Sat, 30 Oct 1999 00:14:11 -0400
  • Organization: debis Systemhaus
  • References: <>
  • Sender: owner-wri-mathgroup at

>Krumm, Anton J schrieb:
>> I would like to plot two functions with "DisplayTogether" in
>> one graphics. However, I would need a second y-axis since
>> the numbers of the second function are much smaller than
>> those of the first function.
>> Is there any way to add a second y-axis and relate a
>> function to that axis?
>> A.Krumm

yesterday I wrote:
> Dear Anton,
> However, to me, the scaling that _Mathematica_ applies for plotting
> graphics is absolutely unpredictable. It not only depends on the plot
> ranges and the embellishments, but also on the details of the graphs
> (the functions) and on the setting of arbitrary (and seemingly
> unrelated) options.
> Nonetheless I managed to do it with an example, and if you need, you may
> proceed along these lines to get to where you want, if you're ready to
> invest a modest amount of trial and error.

Now by chance I found a way to combine the graphics with different
y-scaling, that (for my example) comes out perfectly right without any
trials further needed:

p1=Plot[BesselJ[1,2*Pi*x], {x, 0, 6}, PlotRange->{Automatic,
{-0.6,0.6}}, PlotStyle -> Hue[0.77]]

p2=Plot[(x*Cos[2*Pi*x])/E^x, {x, 0, 6},
PlotRange->{Automatic,{-0.4,0.4}}, PlotStyle -> Hue[0.13], AxesOrigin ->
{6., 0.}]

and then

Show[Graphics[{Rectangle[Scaled[{0.05, 0}, {0, 0}], Scaled[{0.97, 1},
{0, 0}], FullGraphics[p1]], Rectangle[Scaled[{0.05, 0}, {0, 0}],
Scaled[{0.97, 1}, {0, 0}], FullGraphics[p2]]}]]

In this case both x-axes (zero and scale) and y-zero match exactly. What
seems to be necessary to happen so, is the special PlotRange
specification: it should be Automatic for the x-axis in both graphs
(specifying {0,6} wouldn't do!! -- erratic!!) and be explicit with the
y-range (not Automatic).

With frames I wasn't quite as lucky, however I made some progress:

p2x = Plot[(x*Cos[2*Pi*x])/E^x, {x, 0, 6},
PlotRange->{Automatic,{-0.39,0.39}}, PlotStyle -> Hue[0.13], AxesOrigin
-> {6., 0.}, 
   Frame->True,  FrameTicks->{None,Automatic,None,None}]

This was only to extract the FrameTicks option (to later put it to the
right side).

ft = FullOptions[p2x, FrameTicks][[2]];

p2 = Plot[(x*Cos[2*Pi*x])/E^x, {x, 0, 6},
PlotRange->{Automatic,{-0.39,0.39}}, PlotStyle -> Hue[0.13], AxesOrigin
-> {6., 0.}, 
   Frame->True, FrameTicks->{Automatic,None,Automatic,ft}]

a = Sqrt[2];
p1 = Plot[(a*x*Sin[2*Pi*x])/E^x, {x, 0, 6}, PlotRange->{Automatic, a
{-0.39,0.39}}, PlotStyle -> Hue[0.77], Frame->True,

Now if we combined

Show[Graphics[Rectangle[Scaled[{0.05, 0}, {0, 0}], Scaled[{0.95, 1}, {0,
0}], FullGraphics[p1]]], 
     Graphics[Rectangle[Scaled[{0.05, 0}, {0.0115, 0}], Scaled[{0.95,
1}, {0.0115,0}], FullGraphics[p2]]]]

We will have only a very small relative shift in the x-direction, but
x-scale and y-shift will be already ok, so we could quickly find by
trial the necessary correction (0.0115 x-shift in user coordinates in
this case). The display coordinates 0.05 (instead of 0) and 0.95
(instead of 1) for the rectangles are uncritical and only to leave
enough space for the ticks and tick labels in the final combined display

Yesterday I proposed to scrutinize the postscript string to find the
display corrections when combining the graphs; perhaps another way might
be easier: scale and shift (let's call it the second) graph to the same
common user coordinates, directly combine with show, read-off the
y-range, transform that with the inverse, plot the unscaled second graph
with that back-transformed y-range option, extract the FrameTicks Option
for y, transform that again forward (but only the coordinates, keep the
labels), and then add that as a Line primitive to the combined plot (it
containes no display coordinates). This sounds more complicated than it
is; in fact, the only thing we really need is a function to generate
arbitrary (scaled) ticks effortless and *add* that to the graph at
convenient places (right and left, two at the left, mirrored en face,

I hope to have helped and not confused anyone too much,
kind regards

  • Prev by Date: Re: drawing tilted ellipses?
  • Next by Date: Assumptions in Integrate
  • Previous by thread: Re: second y-axis in a graph
  • Next by thread: Re: second y-axis in a graph