如何使用从函数调用的 matplotlib 进行绘图?
我目前有一个主脚本,调用一个函数。该函数的部分功能是绘制两个图表。如果我在末尾包含 show()
,该脚本就可以工作,但有点烦人的是,脚本会暂停,直到我关闭图表,或者我必须等到程序运行后才能查看图表。完成的。因此我觉得我需要使用 ion()
/ioff()
。如果我从 shell 调用该函数,下面的代码片段会起作用,但如果我从脚本调用它,则不起作用。
#Plot the graphs.
ion()
firstplot = plot(GridAround[Mode], LogTheory[Mode], '.')
secondplot = plot(GridAround[Mode], NormalApprox[Mode])
draw()
ioff()
我的问题很简单:如何在函数内进行绘图,而不使用 show() (这将暂停脚本或仅在最后显示图形)?
我提前道歉,这个问题肯定在网上某个地方有答案,但经过几个小时的搜索,我还没有找到它。
I currently have a main script, calling a function. Part of that functions functionality is plotting two graphs. The script works if I include show()
at the end, but it is a bit annoying that either the script pauses until I close the graph or I have to wait to look at the graphs until the program is finished. Hence I feel like I need to use ion()
/ioff()
. The code snippet below works if I call the function from the shell, but not if I call it from the script.
#Plot the graphs.
ion()
firstplot = plot(GridAround[Mode], LogTheory[Mode], '.')
secondplot = plot(GridAround[Mode], NormalApprox[Mode])
draw()
ioff()
My question is simply: How do I plot from within a function, without using show()
(which will pause the script or only show the graphs at the end)?
I apologize in advance, this question surely must have an answer somewhere on the web, but after a couple of hours of searching, I have not been able to find it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用不同的渲染后端,但是,您需要使用
savefig
将绘图保存为文件,而不是使用show
You can use a different rendering backend, however, you will need to save the plot as a file with
savefig
instead of usingshow