       Re: second y-axis in a graph

• To: mathgroup at smc.vnet.net
• Subject: [mg20551] Re: [mg20515] second y-axis in a graph
• From: "Wolf, Hartmut" <hwolf at debis.com>
• Date: Sat, 30 Oct 1999 00:14:02 -0400
• Organization: debis Systemhaus
• References: <199910270605.CAA10519@smc.vnet.net.>
• Sender: owner-wri-mathgroup at wolfram.com

```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*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
y-axis.

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
coordinates):

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}],
FullGraphics[p2]]}]]

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
x-scaling.

If you instead define

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

In:=
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:

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

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,
apologizing,
Hartmut

```

• 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