MathGroup Archive 1999

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

Search the Archive

Re: second y-axis in a graph

  • To: mathgroup at
  • Subject: [mg20551] Re: [mg20515] second y-axis in a graph
  • From: "Wolf, Hartmut" <hwolf at>
  • Date: Sat, 30 Oct 1999 00:14:02 -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-axes 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

Dear Anton,

I would be happy to write that function "DisplayTogether" -- you are not
alone with your wishes. The basis idea is of course, as you said, to
display together two (say arbitrary) graphes. Most needs would comply
with demanding for the same range and scale of the x-axes (but having
solved the problem for y, that will be a paradigma for x too).

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.

What did I do? I plotted two (related) functions:

p1 = Plot[(Sqrt[2]*x*Sin[2*Pi*x])/E^x, {x, 0, 6}, 
     PlotStyle -> Hue[0.77], 
     Axes -> {None, Automatic}, AxesStyle -> {Hue[0.77]}]

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

Please note, there are different scales (and ranges) for the respective

p1 has its y-scale to the left, but no x-axes drawn, p2 has an x-axes
and its y-scale to the right. We combine these plots attempting to keep
their displayed sizes (Show doesn't do, because it combines at the user

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

This graph already comes close to what we want, the scaling is grossly
right, you can see the labeled scales to the left and to the right, the
colour shows, what belongs to what. The scaled coordinates 0.05 (instead
of 0) and 0.95 (instead of 1) made these different y-scales (fully)
visible; perhaps sometimes you may have to increase those margins.

Yet with this graph the coordinate centers do not match exacly, and the
x-scaling is not identical. However you can read off the differences in
user coordinates (if you enlarge the graph) with sufficient precision.
So after only two trials I arrived at

Show[Graphics[{Rectangle[Scaled[{0.05, 0}, {0, 0}], Scaled[{0.95, 1},
{0, 0}], FullGraphics[p1]], 
    Rectangle[Scaled[{0.05, 0}, {-0.01, 0.018}], Scaled[{0.95, 1},
{0.022, 0.018}], FullGraphics[p2]]}]]

which seems to be good enough to the naked eye.

The advantage of this method is that there is only one singular x-axis.
The disadvantage is, that you have to fine tune y-shift and x-shift and

If you instead define

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

Plot[(2*x*Sin[2*Pi*x])/E^x, {x, 0, 6}, PlotStyle -> Hue[0.77]]

then the x-axes already scale perfectly right, and you have only to
adapt for the y-coordinate of the origin:

Show[Graphics[{Rectangle[Scaled[{0.05, 0}, {0, 0}], Scaled[{1, 1}, {0,
0}], FullGraphics[Out[164]]], 
    Rectangle[Scaled[{0.05, 0}, {0, -0.02}], Scaled[{1, 1}, {0, -0.02}],

If you are at your goal, both printed x-axes match exactly.

For many applications, say display systolic and diastolic blood pressure
together with the pulse rate (from the same measurement), there exists
no common origin for pressure and pulse. To the contrary, you possibly
want to shift the curves up or down for clarity, then perhaps the latter
method will succeed with just the minimal necessary manual intervention.

Perhaps even a better idea would be to go through a siminar procedure
with Frames->True.

Of course I didn't solve the computational problem; an idea would be:
add to the primary graphs some (invisible, hidden or white) objects
(with known user coordinates), display the graph to some channel, and
from there try to readoff the postscript coordinates of the markers,
then calculate the necessary scaling.

But perhaps this all is too complicated nonsense, 

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