Re: Plotting

• To: mathgroup at smc.vnet.net
• Subject: [mg28736] Re: [mg28694] Plotting
• From: Matt.Johnson at autolivasp.com
• Date: Fri, 11 May 2001 03:38:51 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Yahsi-

That is one function that they should definitely include.  But they don't.
So here's one for two functions.  It will take some tailoring to get the
best output and the programming isn't too pretty, but it works.

-matt

In[1]:=
Off[General::spell, General::spell1]
In[2]:=
f1[x_] := Sin[x];
f2[x_] := x^2;
In[4]:=
(*  f1 will be plotted on the left - hand axis,
f2 will be plotted on the right -
hand axis. They both must be functions in just one variable.
xmin and xmax specify the plot range.  Depending on the functions,
the function may need to be tweaked for the appropriate minimum and
maximum \
values.  *)

twoAxisPlot[f1_, f2_, xmin_, xmax_] :=
Module[{ptext1, ptext2, plotPoints1, plotPoints2, min1, min2, max1,
max2,
p1, p2, rhsTicks},
(* Get the textual form of each plot and find the max and min y -
values. *)
ptext1 =
Plot[f1[x], {x, xmin, xmax}, DisplayFunction -> Identity] //
InputForm;
ptext2 =
Plot[f2[x], {x, xmin, xmax}, DisplayFunction -> Identity] //
InputForm;
plotPoints1 = Cases[Cases[ptext1, _Line, \[Infinity]], _List, 2]
[[1]];
plotPoints2 = Cases[Cases[ptext2, _Line, \[Infinity]], _List, 2]
[[1]];
min1 = Min[Transpose[plotPoints1][[2]]];
max1 = Max[Transpose[plotPoints1][[2]]];
min2 = Min[Transpose[plotPoints2][[2]]];
max2 = Max[Transpose[plotPoints2][[2]]];
(*  Create the plots,
with the second plot scaled to the left - hand axis *)
p1 = Plot[f1[x], {x, xmin, xmax}, DisplayFunction -> Identity];
p2 =
Plot[f2[x]*(max1 - min1)/(max2 - min2) + max1 -
max2*(max1 - min1)/(max2 - min2), {x, xmin, xmax},
DisplayFunction -> Identity];

(* Define the RHS tickmarks *)
rhsTicks =
Map[Chop[#] &,
Transpose[{Range[min1, max1, (max1 - min1)/5],
Range[min2, max2, (max2 - min2)/5]}]];

(*  Combine and display the plots  *)
Show[p1, p2, Frame -> True, DisplayFunction -> \$DisplayFunction,
FrameTicks -> {Automatic, Automatic, None, rhsTicks}];];
In[6]:=
twoAxisPlot[f1, f2, 0, 10]

Yahsi <yahsi at marun.edu.tr> on 05/10/2001 05:54:41 AM

cc:
Subject: [mg28736]  [mg28694] Plotting

Hi,

I have a question regarding the plot. Suppose that you have two separate
functions and you would like to plot them on the same graph. Both are not
so
scaled to plot it using the single y axis. Sometimes we need to draw the
plots of two functions (f1 and f2) on two separate y axis (one is on the
left as usual and the other on the right). This features are common even
very primitive versions of the graph programs such as grapher and excel. I
am not able to figure out how to do this in Mathematica.
Uður

Assoc. Prof. Dr. Uður Yahþi
Marmara University Faculty of Arts and Sciences
Physica Department, 81040 Goztepe, Istanbul-TURKEY
yahsi at marun.edu.tr

**************************************************************************************************************************************
All Autoliv e-mails remain Company property and shall be used for
business-related purposes only.   Do not forward any material contained
within this e-mail without prior, written permission from the author's
manager.  Autoliv disclaims all responsibility and accepts no liability
(including negligence) for the consequences for any person acting, or
refraining from acting on this information prior to the receipt by those
persons of subsequent written confirmation.
**************************************************************************************************************************************

```