Mathematica 操作图:缩放轴

发布于 2024-12-25 23:45:16 字数 486 浏览 4 评论 0原文

假设我已经设置了以下函数 f[a,b,c] ,我希望在改变 ab 时绘制

f[a_,b_,c_]:=a b c Exp[a b]

Manipulate[
Plot
[
f[a,b,c],
{c,0,1},
PlotRange->{{0,0.05},Automatic}
],
{a,0,1},
{b,0,1}
]

它是否可能当我固定横坐标观看范围时,纵坐标会自动缩放吗?您会注意到上面的代码,当改变 ab 时,纵坐标会自动缩放,就像我正在查看 < 的整个范围一样。代码>{c,0,1}。我希望它仍然能够处理从 0 到 1 的 c ,但是如果我想查看该图的较小部分,例如从 0 到 0.05 的 c ,仍然可以垂直轴缩放正确。谢谢大家的帮助。

Say I have set up the following function f[a,b,c] which I wish to plot while varying a and b

f[a_,b_,c_]:=a b c Exp[a b]

Manipulate[
Plot
[
f[a,b,c],
{c,0,1},
PlotRange->{{0,0.05},Automatic}
],
{a,0,1},
{b,0,1}
]

Is it possible to have the ordinate scaled automatically when I fix the abscissa viewing range? You'll notice with the code above that when varying a and b the ordinate does scale automatically as if I were viewing the whole range of {c,0,1}. I would like it to still handle c from 0 to 1, but if I want to view a smaller section of this plot, say c from 0 to 0.05, still have the vertical axis scaled correctly. Thank you all for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月野兔 2025-01-01 23:45:16

Artes Docendo 建议的一个变体:

Manipulate[
 Plot[f[a, b, c], {c, 0, Evaluate@d}, 
  PlotRange -> {{0, Evaluate@d}, Full}], {a, 0., 1.}, {b, 0., 1.}, {d, 
  0.05, 1.}]

注意 Evaluate 强制将机器精度值提供给 Plot 函数,然后再实际尝试绘制某些内容。

在这种情况下,对于 y 轴 PlotRange,我更喜欢使用 Full 而不是 Automatic,因为那样你就知道它永远不会以某种方式裁剪绘图隐藏部分曲线。

A variant on Artes Docendo's suggestion:

Manipulate[
 Plot[f[a, b, c], {c, 0, Evaluate@d}, 
  PlotRange -> {{0, Evaluate@d}, Full}], {a, 0., 1.}, {b, 0., 1.}, {d, 
  0.05, 1.}]

Notice the Evaluate to force the machine-precision value to be fed to the Plot function before it actually tries to draw something.

I prefer Full instead of Automatic for the y-axis PlotRange in cases like this, because then you know it will never crop the plot in ways that hide parts of the curve.

你没皮卡萌 2025-01-01 23:45:16

这是许多可能的解决方案之一:

f[a_, b_, c_] := a b c Exp[a b]
Manipulate[ Plot[f[a, b, c], {c, 0, d}, PlotRange -> Automatic], 
            {a, 0, 1}, {b, 0, 1}, {d, 0.1, 1}, Initialization :> (d := 0.1)]

但是您的示例不是很有启发性,要了解它如何更好地工作,请尝试类似的方法
这 :

g[a_, b_, c_] := 3 (a - 0.5) Cos[4 Pi (a - c)] Sin[8 Pi (c - 0.5)] Cos[6 Pi (b - c)]

Manipulate[
           Plot[g[a, b, c], {c, 0, d}, PlotRange -> Automatic],
           {a, 0, 1}, {b, 0, 1}, {d, 0.1, 1}, 
           Initialization :> (a := 0.4; b := 0.4; d := 0.5)]

Here is one of many possible solutions :

f[a_, b_, c_] := a b c Exp[a b]
Manipulate[ Plot[f[a, b, c], {c, 0, d}, PlotRange -> Automatic], 
            {a, 0, 1}, {b, 0, 1}, {d, 0.1, 1}, Initialization :> (d := 0.1)]

However your example is not very instructive, to see how it works better try something like
this :

g[a_, b_, c_] := 3 (a - 0.5) Cos[4 Pi (a - c)] Sin[8 Pi (c - 0.5)] Cos[6 Pi (b - c)]

Manipulate[
           Plot[g[a, b, c], {c, 0, d}, PlotRange -> Automatic],
           {a, 0, 1}, {b, 0, 1}, {d, 0.1, 1}, 
           Initialization :> (a := 0.4; b := 0.4; d := 0.5)]
深者入戏 2025-01-01 23:45:16

看看这是否符合您的要求。我只是使用 ListPlot 而不是绘图。

但我不确定你在做什么,因为你正在为 c 从 0 到 1 绘制 f ,然后将 x 范围设置为仅从 0 到 0.05?为什么不直接使用 {c,0,0.05} 绘制 f 呢?也许我错过了一些东西。

无论如何,这就是我

 Manipulate[

 xmax = 0.05;
 y = Table[f[a, b, c], {c, 0, xmax, 0.01}];
 max = Max[y];
 min = Min[y];

 Plot[f[a, b, c], {c, 0, 1},
  PlotRange -> {{0, xmax}, {min, max}}, ImagePadding -> 30],

 {a, 0, 1},
 {b, 0, 1},
 Initialization :>
  (
   f[a_, b_, c_] := a b c Exp[a b]
   )

 ]

刚刚想到的edit(1)

,为了使上述更有效,是使用第一个 Table 命令,也生成数据本身,而不仅仅是找到绘图范围的最大值/最小值。然后使用 ListPlot 而不是 Plot。这应该更快,这样函数 f 的采样只发生一次而不是两次?

所以这是第二个版本

Manipulate[xmax = 0.05;

 data = Table[{c, f[a, b, c]}, {c, 0, xmax, 0.01}];
 max = Max[data[[All, 2]]];
 min = Min[data[[All, 2]]];

 ListPlot[
  data,
  PlotRange -> {Automatic, {min, max}},
  Joined -> True,
  ImagePadding -> 30
  ],

 {a, 0, 1},
 {b, 0, 1},
 Initialization :>
  (
   f[a_, b_, c_] := a b c Exp[a b]
   )
 ]

See if this does what you want. I simply use ListPlot instead of plot.

But I am not sure what you are doing, as you are plotting f for c from 0 to 1, but then setting the x-range to only be from 0 to 0.05? Why not then just plot f using {c,0,0.05}? May be I am missing something.

Anyway, here is what I have

 Manipulate[

 xmax = 0.05;
 y = Table[f[a, b, c], {c, 0, xmax, 0.01}];
 max = Max[y];
 min = Min[y];

 Plot[f[a, b, c], {c, 0, 1},
  PlotRange -> {{0, xmax}, {min, max}}, ImagePadding -> 30],

 {a, 0, 1},
 {b, 0, 1},
 Initialization :>
  (
   f[a_, b_, c_] := a b c Exp[a b]
   )

 ]

edit(1)

it just occurred to me, to make the above more efficient, is to use the first Table command, to generate the data itself as well, and not just find the max/min of the plot range. And then use ListPlot instead of Plot. This should be faster, so that sampling of the function f only happens once instead of 2 times?

So here is second version

Manipulate[xmax = 0.05;

 data = Table[{c, f[a, b, c]}, {c, 0, xmax, 0.01}];
 max = Max[data[[All, 2]]];
 min = Min[data[[All, 2]]];

 ListPlot[
  data,
  PlotRange -> {Automatic, {min, max}},
  Joined -> True,
  ImagePadding -> 30
  ],

 {a, 0, 1},
 {b, 0, 1},
 Initialization :>
  (
   f[a_, b_, c_] := a b c Exp[a b]
   )
 ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文