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
ronaldc1 (2)
3/2/2004 5:25:19 AM
comp.soft-sys.math.mathematica 28821 articles. 0 followers. Follow

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
pg.hahn (7)
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[1]:=
g1 = Plot[Cos[30/(1 + x^2)], {x, -10, 10}, Frame -> True]

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

We scale g2:

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

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

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


....and combine

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



But I'm sure, David Park has something better to offer at
http://home.earthlink.net/~djmp/Mathematica.html


--
Hartmut Wolf

0
Hartmut.Wolf (216)
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
djmp@earthlink.net
http://home.earthlink.net/~djmp/ 





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[1]:=
g1 = Plot[Cos[30/(1 + x^2)], {x, -10, 10}, Frame -> True]

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

We scale g2:

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

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

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


....and combine

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



But I'm sure, David Park has something better to offer at
http://home.earthlink.net/~djmp/Mathematica.html


--
Hartmut Wolf


0
djmp (1214)
3/3/2004 7:30:59 AM
Reply: