Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Plot axis length and size ratio (TwoPlot revive)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121581] Re: Plot axis length and size ratio (TwoPlot revive)
  • From: Jaebum Jung <jaebum at wolfram.com>
  • Date: Wed, 21 Sep 2011 05:32:24 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

You could also define function like this:

TwoAxisPlot[{f_, g_}, {x_, x0_, x1_}, opts : OptionsPattern[]] := 
 Module[
  {f0, f1, g0, g1, scale, gticks, style, ymid},
  style = PlotStyle /. {opts} /. (PlotStyle -> {Blue, Red});
  {f0, f1} = 
   Options[Plot[f, {x, x0, x1}, Frame -> True, opts], PlotRange][[1, 
    2, 2]];
  {g0, g1} = 
   Options[Plot[g, {x, x0, x1}, Frame -> True, opts], PlotRange][[1, 
    2, 2]];
  If[f0 < 0 && f1 > 0, ymid = f0 + ((f1 - f0) (0 - g0))/(g1 - g0), 
   ymid = 0];
  
  scale[y_] := f0 + ((f1 - f0) (y - g0))/(g1 - g0) - ymid;
  gticks = 
   AbsoluteOptions[
      Plot[g, {x, x0, x1}, 
       PlotRange -> Evaluate[1/2 {{3, -1}, {-1, 3}}.{g0, g1}], 
       Frame -> True], FrameTicks][[1, 2, 
     2]] /. {{tk_?NumericQ, lb_?NumericQ, _, _} :> {scale[tk], 
       lb, {.01, 0}}, {tk_?NumericQ, "", _, _} :> {scale[tk], 
       "", {.005, 0}}};
  Plot[{f, scale[g]}, {x, x0, x1},
   Frame -> True,
   AxesOrigin -> {x0, Min[f0, scale[g0]]},
   FrameTicks -> {{Automatic, gticks}, {Automatic, None}},
   FrameStyle -> {style, {{}, {}}},
   PlotStyle -> style,
   opts
   ]
  ]

TwoAxisPlot[{x Sin[x],  x (x - 10) Cos[x]}, {x, 0, 10}]

TwoAxisPlot[{x Sin[x],  x (x - 10) Cos[x]}, {x, 0, 10}, GridLines -> Automatic]

Sincerely,
Jaebum

----- Original Message -----
From: "matyigtm" <matyigtm at gmail.com>
To: mathgroup at smc.vnet.net
Sent: Monday, September 19, 2011 6:06:43 AM
Subject: [mg121581] Plot axis length and size ratio (TwoPlot revive)

Hi,

I want to combine two plots with the same "x" axis into one graphic.
Because the two plots have different magnitude, I choosed the "Inset"
command inside of one of the two plots. I want to put the two "y" axes
on left and right for the two plots, resp. (aka. TwoPlot)
My problem is that the two plots take different sizes when the numbers
written at the ticks on the "y" axis get different length, or when
AxesLabel is used. I've understood the the area of the plot is
extended by PlotRangePadding and with ImagePadding, but I cannot
retrieve the latter from the plots to get the right sizes.

The Inset command help a lot to position the origins of the two plots,
but its "size" parameter looses any relation to the coordinates used
in the plots.

The following example illustrates the problem, the hardcoded number
1.045 is to be programmed to give good results.

plt2 = Plot[x (x - 10) Cos[x], {x, 0, 10}, PlotStyle -> Red,
AxesOrigin -> {10, 0}, AxesStyle -> Red];
Plot[x Sin[x], {x, 0, 10},  Epilog -> Inset[plt2, {0, 0}, {0, 0}, 10 *
1.045]

Any idea to align the two plots is appreciated,
Matyi








  • Prev by Date: Re: Problem in "block cutting"
  • Next by Date: Re: "Traveling salesman on a hemisphere" problem
  • Previous by thread: Re: Plot axis length and size ratio (TwoPlot revive)
  • Next by thread: Re: Plot axis length and size ratio (TwoPlot revive)