Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #5110

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

From Jaebum Jung <jaebum@wolfram.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Plot axis length and size ratio (TwoPlot revive)
Date 2011-09-21 09:33 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j5cb27$crg$1@smc.vnet.net> (permalink)

Show all headers | View raw


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@gmail.com>
Sent: Monday, September 19, 2011 6:06:43 AM
Subject: 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




Back to comp.soft-sys.math.mathematica | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: Plot axis length and size ratio (TwoPlot revive) Jaebum Jung <jaebum@wolfram.com> - 2011-09-21 09:33 +0000
  Re: Plot axis length and size ratio (TwoPlot revive) matyigtm <matyigtm@gmail.com> - 2011-09-23 07:46 +0000

csiph-web