f

#### How to combine two functions in one plot using a left and a right y-axis

```Hi all,
Can someone help me with the following problem. I want to combine two
functions with different scales in one plot, using a left and a right
y-axis. Is there a way to do this?
Thanks,
Ronald

``` 0 3/2/2004 5:25:19 AM comp.soft-sys.math.mathematica  28821 articles. 0 followers. 3 Replies 538 Views Similar Articles

[PageSpeed] 10

```Ronald Cornelussen wrote:

> Hi all,
> Can someone help me with the following problem. I want to combine two
> functions with different scales in one plot, using a left and a right
> y-axis. Is there a way to do this?
> Thanks,
> Ronald
>
Hi Ronald,

a colleague of mine wrote a package for this task. Currently, it has
more or less the state of a beta-version. I can send it to you if you
want. Unfortunately the documentation is in only in german, but as you
are from the Netherlands, may be this is no problem for you.

Please let me know if you want me to send the package to you.

Peter

``` 0 3/3/2004 12:13:23 AM
```>-----Original Message-----
>From: Ronald Cornelussen [mailto:ronaldc@science.uva.nl]
>Sent: Tuesday, March 02, 2004 6:14 AM
>To: mathgroup@smc.vnet.net
>Subject:  How to combine two functions in one plot
>using a left
>and a right y-axis
>
>
>Hi all,
>Can someone help me with the following problem. I want to combine two
>functions with different scales in one plot, using a left and a right
>y-axis. Is there a way to do this?
>Thanks,
>Ronald
>

The basic idea is to scale (at least one of the graphs) + the corresponding
ticks.
For example:

In:=
g1 = Plot[Cos[30/(1 + x^2)], {x, -10, 10}, Frame -> True]

In:=
g2 = Plot[30/(1 + x^2), {x, -10, 10}, Frame -> True, PlotStyle -> Hue,
Axes -> False, FrameTicks -> {Automatic, None, None, True}]

We scale g2:

In:= scale[y_] := -.6 + y/15

In:= scaledTicks =
FullOptions[g2, FrameTicks][] /. {y_Real, label_, len_,
style_} :> {scale[y], label, len, style};

In:= scaledGraph = Graphics[g2[]] /. {x_Real, y_Real} :> {x,
scale[y]};

....and combine

In:=
Show[g1, scaledGraph,
FrameTicks -> {Automatic, FullOptions[g1, FrameTicks][], None,
scaledTicks}]

But I'm sure, David Park has something better to offer at

--
Hartmut Wolf

``` 0 3/3/2004 12:19:27 AM
```Thank you Hartmut.

So here is how I would do it with DrawGraphics...

Needs["DrawGraphics`DrawingMaster`"]

Draw2D[
{Draw[Cos[30/(1 + x^2)], {x, -10, 10}],
Red,
Draw[30/(1 + x^2), {x, -10, 10}] /.
DrawingTransform[#1 &, -0.6 + #2/15 &]},
Frame -> True,
FrameTicks ->
{Automatic,
CustomTicks[Identity, {-1, 1, 1, 5}],
Automatic,
CustomTicks[-0.6 + #1/15 &, {0, 30, 10, 5},
CTNumberFunction -> (StyleForm[#, FontColor -> Red] &)]},
Background -> Linen,
PlotLabel -> "Two Scale Plot",
ImageSize -> 450];

David Park

From: Wolf, Hartmut [mailto:Hartmut.Wolf@t-systems.com]

>From: Ronald Cornelussen [mailto:ronaldc@science.uva.nl]
>Sent: Tuesday, March 02, 2004 6:14 AM
>To: mathgroup@smc.vnet.net
>Subject:  How to combine two functions in one plot
>using a left
>and a right y-axis
>
>
>Hi all,
>Can someone help me with the following problem. I want to combine two
>functions with different scales in one plot, using a left and a right
>y-axis. Is there a way to do this?
>Thanks,
>Ronald
>

The basic idea is to scale (at least one of the graphs) + the corresponding
ticks.
For example:

In:=
g1 = Plot[Cos[30/(1 + x^2)], {x, -10, 10}, Frame -> True]

In:=
g2 = Plot[30/(1 + x^2), {x, -10, 10}, Frame -> True, PlotStyle -> Hue,
Axes -> False, FrameTicks -> {Automatic, None, None, True}]

We scale g2:

In:= scale[y_] := -.6 + y/15

In:= scaledTicks =
FullOptions[g2, FrameTicks][] /. {y_Real, label_, len_,
style_} :> {scale[y], label, len, style};

In:= scaledGraph = Graphics[g2[]] /. {x_Real, y_Real} :> {x,
scale[y]};

....and combine

In:=
Show[g1, scaledGraph,
FrameTicks -> {Automatic, FullOptions[g1, FrameTicks][], None,
scaledTicks}]

But I'm sure, David Park has something better to offer at 0 