Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5110 > unrolled thread
| Started by | Jaebum Jung <jaebum@wolfram.com> |
|---|---|
| First post | 2011-09-21 09:33 +0000 |
| Last post | 2011-09-23 07:46 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
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
| From | Jaebum Jung <jaebum@wolfram.com> |
|---|---|
| Date | 2011-09-21 09:33 +0000 |
| Subject | Re: Plot axis length and size ratio (TwoPlot revive) |
| Message-ID | <j5cb27$crg$1@smc.vnet.net> |
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
[toc] | [next] | [standalone]
| From | matyigtm <matyigtm@gmail.com> |
|---|---|
| Date | 2011-09-23 07:46 +0000 |
| Message-ID | <j5hdhb$7jg$1@smc.vnet.net> |
| In reply to | #5110 |
Thanks for the ideas.
I've already found the TwoAxisPlot version, but I didn't like that it
had to plot three times the diagrams in total, which is time consuming
in my occasion.
In Ray's version, which short enough to be usable, its hard to guess
the scale factor if I need to make several "two plots" in loops
(hundreds of plots), which motivates me to make a function that
retains most of the Plot function's parameters, options.
Upon your ideas I've created:
Options[TwoPlot] = {PlotOptions1 -> {}, PlotOptions2 -> {},
PlotOptionsAll -> {}, PlotOptionsFrame -> {}, PlotFcn -> Plot,
Scale -> Automatic};
TwoPlot[{f_, g_}, {x_, xmin_, xmax_},
opts : OptionsPattern[{TwoPlot, Graphics}]] :=
Module[{plt1, plt2, absOpt1, absOpt2, absOptTmp, fcnTmp, lenF, lenG,
a, b, range1, range2, rangeDst, scale, ticks2, base = 0},
(* number of elements in f and g *)
{lenF, lenG} = (If[ListQ[#], Length[#], 1] &) /@ {f, g};
plt1 = (OptionValue@PlotFcn)[f, {x, xmin, xmax},
Evaluate@OptionValue@PlotOptions1,
Evaluate@OptionValue@PlotOptionsAll];
absOpt1 = AbsoluteOptions[plt1];
plt2 = (OptionValue@PlotFcn)[g, {x, xmin, xmax},
Evaluate@OptionValue@PlotOptions2,
Evaluate@OptionValue@PlotOptionsAll,
PlotStyle ->
ColorData[1, "ColorList"][[lenF + 1 ;; lenF + lenG]]];
absOpt2 = AbsoluteOptions[plt2];
(* scale factor *)
range1 = PlotRange /. absOpt1;
range2 = PlotRange /. absOpt2;
If[TrueQ[OptionValue@Scale == Automatic],
(*then*)
scale = Min[
Table[If[range2[[2, i]] != 0., range1[[2, i]]/
range2[[2, i]], \[Infinity]], {i, 1, 2}]],
(* else *) scale = OptionValue@Scale];
rangeDst =
Table[({Min, Max}[[i]])@{range1[[2, i]],
range2[[2, i]] scale}, {i, 1, 2}];
(* creates internal plot to create new ticks for plot nr .2 *)
a = (rangeDst[[2]] /scale - rangeDst[[1]]/ scale)/(xmax - xmin);
fcnTmp = a x + (rangeDst[[1]]/ scale - a xmin);
absOptTmp =
AbsoluteOptions[(OptionValue@PlotFcn)[fcnTmp, {x, xmin, xmax},
PlotRange -> {All, rangeDst/ scale}, PlotPoints -> 2,
MaxRecursion -> 0, Evaluate@OptionValue@PlotOptions2,
Evaluate@OptionValue@PlotOptionsAll]];
(* rescale the ticks *)
ticks2 = (Ticks /. absOptTmp)[[
2]] /. {v_, label_, len_, style_} -> {v scale , label, len,
style};
(* create the overlaid plots *)
Graphics[{Drop[plt1[[1, 1]], 2],
Scale[Drop[plt2[[1, 1]], 2], {1, scale}, {xmin, base}]},
FilterRules[{opts}, Options[Graphics]],
AspectRatio -> (AspectRatio /. absOpt1), Frame -> True,
FrameTicks -> {{Automatic, ticks2}, {All, Automatic}},
FrameStyle -> {{(AxesStyle /. absOpt1)[[
2]], (AxesStyle /. absOpt2)[[2]]}, {(AxesStyle /. absOpt1)[[
1]], Automatic}},
PlotRange -> {Automatic, rangeDst}]
];
SetAttributes[TwoPlot, HoldAll];
which applies in different manners:
TwoPlot[{x (x - 10) Sin[x], 10 x (x - 10) Cos[x]}, {x, 0, 10}]
TwoPlot[{{x (x - 10) Sin[x], 20 Cos[x]}, 10 x (x - 10) Cos[x]}, {x, 0,
10}, FrameLabel -> {{"y1", "y2"}, {, }}]
I thought that an easy conversion to functions LogPlot,LogLinearPlot
etc.. would be easy, but I found that not.
I should convert the ticks accordingly to the Plot function used:
TwoPlot[{{x (x - 10) Sin[x], 20 Cos[x]}, 10 x (x - 10) Cos[x]}, {x, 1,
10}, FrameLabel -> {{"y1", "y2"}, {, }}, PlotFcn -> LogPlot]
Matyi
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web