如何在mathematica中显示循环内的绘图

发布于 2024-10-31 04:05:44 字数 467 浏览 1 评论 0原文

我想知道你是否有好方法在 mma 中显示循环内的绘图。通常,Plot函数的输出不会显示,例如下面的代码:

For[i = 1, i <= 10, i++, Plot[Sin[i*x] , {x, -Pi, Pi}]]

感谢您的帮助。

编辑

关于我的上一个问题,我已经有了 For循环,例如,像这样 For[i = 1, i <= 10, i++, Plot[Sin[i*x], {x, -Pi, Pi}]]。鉴于这一事实,我希望在 For 循环内有类似“按任意键继续...”的内容,然后每次按任意随机键时刷新绘图。有人可以给出完整的工作代码吗?

I am wondering if you have good ways to show plots inside a loop in mma. Usually, the output of Plot function is not shown, for example in the following code:

For[i = 1, i <= 10, i++, Plot[Sin[i*x], {x, -Pi, Pi}]]

Thanks for your help.

Edit

In connection to my previous question, I already have the For loop, for example, like this For[i = 1, i <= 10, i++, Plot[Sin[i*x], {x, -Pi, Pi}]]. Given this fact, I want to have something like "press any key to continue..." inside the For loop, then refresh the plot every time I press any random key. Could anybody give a complete working code?

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

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

发布评论

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

评论(3

羁〃客ぐ 2024-11-07 04:05:44

只需使用 Print:

For[i = 1, i <= 10, i++, Plot[Sin[i*x], {x, -Pi, Pi}] // Print]

Monitor:

Monitor[For[i = 1, i <= 10, i++, p = Plot[Sin[i*x], {x, -Pi, Pi}]; 
  Pause[0.5]], p]

(此处使用暂停是为了给一些时间来查看绘图;这里的循环非常快。如有必要,请删除)

编辑
根据请求,通过鼠标点击图表来控制的版本(按键需要图表具有焦点,因此您无论如何都需要点击)

Monitor[For[i = 1, i <= 10, , p = Plot[Sin[i*x], {x, -Pi, Pi}]], 
EventHandler[p, {"MouseDown" :> i++}]]

这是一种非常愚蠢的方法。循环不断地重绘绘图。所以,一个稍微(但仍然丑陋)的版本可能是:

s = True;
Monitor[
 For[i = 1, i <= 10, ,
  If[s,
   (* Put main loop body here*) 
   p = Plot[Sin[i*x], {x, -Pi, Pi}] 
   (* end of main body *) ;
   s = False (* prevents continuous re-evaluating main body *)
   ]
  ]
 , EventHandler[p, {"MouseDown" :> (i++; s = True)}]
 ]

Just use Print:

For[i = 1, i <= 10, i++, Plot[Sin[i*x], {x, -Pi, Pi}] // Print]

or Monitor:

Monitor[For[i = 1, i <= 10, i++, p = Plot[Sin[i*x], {x, -Pi, Pi}]; 
  Pause[0.5]], p]

(Pause is used here to give some time to view the plot; the loop is pretty fast here. Remove if necessary)

EDIT
On request a version that is controlled by mouse clicks on the graph (key presses need the graph to have focus so you need to click anyway)

Monitor[For[i = 1, i <= 10, , p = Plot[Sin[i*x], {x, -Pi, Pi}]], 
EventHandler[p, {"MouseDown" :> i++}]]

This is a pretty stupid way to do this. The loop redraws the plot continuously. So, a slightly (but still ugly) version might be:

s = True;
Monitor[
 For[i = 1, i <= 10, ,
  If[s,
   (* Put main loop body here*) 
   p = Plot[Sin[i*x], {x, -Pi, Pi}] 
   (* end of main body *) ;
   s = False (* prevents continuous re-evaluating main body *)
   ]
  ]
 , EventHandler[p, {"MouseDown" :> (i++; s = True)}]
 ]
乖乖兔^ω^ 2024-11-07 04:05:44

只需返回绘图列表,而不是使用 For 循环:

Table[Plot[Sin[i*x], {x, -Pi, Pi}], {i, 1, 10}]

在此处输入图像描述

如果您希望将它们全部连接为一个图,Show[listOfPlots] 是一种方法:

Show[Table[Plot[Sin[i*x], {x, -Pi, Pi}], {i, 1, 10}]]

在此处输入图像描述

更新

这是使用DynamicEventHandler的一种简单方法:

DynamicModule[{i = 1},
 EventHandler[Dynamic[Plot[Sin[i*x], {x, -Pi, Pi}]],
  {"KeyDown" :> i++}
  ]

这是使用Animate制作的稍微更奇特的界面:

Animate[Plot[Sin[i*x], {x, -Pi, Pi}], {i, 1, 10, 1}, AnimationRunning -> False]

Just return a list of the plots, instead of using a For loop:

Table[Plot[Sin[i*x], {x, -Pi, Pi}], {i, 1, 10}]

enter image description here

If you want them all concatenated as one plot, Show[listOfPlots] is one way to do that:

Show[Table[Plot[Sin[i*x], {x, -Pi, Pi}], {i, 1, 10}]]

enter image description here

UPDATE

Here's one simple way using Dynamic and EventHandler:

DynamicModule[{i = 1},
 EventHandler[Dynamic[Plot[Sin[i*x], {x, -Pi, Pi}]],
  {"KeyDown" :> i++}
  ]

And here's a slightly more fancy interface made with Animate:

Animate[Plot[Sin[i*x], {x, -Pi, Pi}], {i, 1, 10, 1}, AnimationRunning -> False]
李白 2024-11-07 04:05:44

如果您确实想让用户在绘图之间按某个键,最简单的方法可能是

For[i = 1, i <= 10, i++, 
    If[!ChoiceDialog[Plot[Sin[i*x], {x, -Pi, Pi}], 
         WindowTitle -> "Plot #" <> ToString[i] 
                                 <> ":  Press OK or Enter to continue"],
    Abort[]]]

在此处输入图像描述

If you really want to have the user press a key between Plots, the simplest way might be

For[i = 1, i <= 10, i++, 
    If[!ChoiceDialog[Plot[Sin[i*x], {x, -Pi, Pi}], 
         WindowTitle -> "Plot #" <> ToString[i] 
                                 <> ":  Press OK or Enter to continue"],
    Abort[]]]

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文