如何在mathematica中显示循环内的绘图
我想知道你是否有好方法在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需使用
Print
:或
Monitor
:(此处使用暂停是为了给一些时间来查看绘图;这里的循环非常快。如有必要,请删除)
编辑
根据请求,通过鼠标点击图表来控制的版本(按键需要图表具有焦点,因此您无论如何都需要点击)
这是一种非常愚蠢的方法。循环不断地重绘绘图。所以,一个稍微(但仍然丑陋)的版本可能是:
Just use
Print
:or
Monitor
:(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)
This is a pretty stupid way to do this. The loop redraws the plot continuously. So, a slightly (but still ugly) version might be:
只需返回绘图列表,而不是使用
For
循环:如果您希望将它们全部连接为一个图,
Show[listOfPlots]
是一种方法:更新
这是使用
Dynamic
和EventHandler
的一种简单方法:这是使用
Animate制作的稍微更奇特的界面:
Just return a list of the plots, instead of using a
For
loop:If you want them all concatenated as one plot,
Show[listOfPlots]
is one way to do that:UPDATE
Here's one simple way using
Dynamic
andEventHandler
:And here's a slightly more fancy interface made with
Animate
:如果您确实想让用户在绘图之间按某个键,最简单的方法可能是
If you really want to have the user press a key between Plots, the simplest way might be