MathGroup Archive 2001

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

Search the Archive

Re: Plotting


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.


Off[General::spell, General::spell1]
f1[x_] := Sin[x];
f2[x_] := x^2;
(*  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,
        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] //
      ptext2 =
        Plot[f2[x], {x, xmin, xmax}, DisplayFunction -> Identity] //
      plotPoints1 = Cases[Cases[ptext1, _Line, \[Infinity]], _List, 2]
      plotPoints2 = Cases[Cases[ptext2, _Line, \[Infinity]], _List, 2]
      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}];];
twoAxisPlot[f1, f2, 0, 10]

Yahsi <yahsi at> on 05/10/2001 05:54:41 AM

Subject: [mg28736]  [mg28694] Plotting


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
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.
Thanks for your replies in advance.

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

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.

  • Prev by Date: Re: Please help!
  • Next by Date: Summary of list of bits responses
  • Previous by thread: RE: Plotting
  • Next by thread: Peculiar behavior of DiscreteDelta