简体   繁体   English

我可以在python交互模式下有多个matplotlib绘图窗口吗?

[英]Can I have multiple matplotlib plot windows in python interactive mode?

I've been using Matlab/octave for a long time, and I'm transiting to NumPy/SciPy. 我已经使用Matlab / octave很长一段时间了,我正在转向NumPy / SciPy。 I find that matplotlib is very similar to figure drawing in Matlab, and it is easy to use. 我发现matplotlib与Matlab中的图形绘图非常相似,并且易于使用。

But, one thing I feel uncomfortable with matplotlib is when I draw a figure using plt.show() , then the process is stuck there, so I cannot type any new commands nor launch another window to draw another figure before closing that window. 但是,有一点我对matplotlib感到不舒服的是当我使用plt.show()绘制一个图形时,然后该过程被卡在那里,因此我无法在关闭该窗口之前键入任何新命令或启动另一个窗口来绘制另一个图形。 For example, if we type the following code, then before closing this window, we cannot type any new command nor launch another window for another plot. 例如,如果我们键入以下代码,那么在关闭此窗口之前,我们不能键入任何新命令,也不能为另一个绘图启动另一个窗口。

import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()

This behavior is very different from Matlab plot. 这种行为与Matlab图非常不同。 We may have multiple figure windows in the Matlab interactive mode. 我们可能在Matlab交互模式中有多个图形窗口。

Can we do the same thing in python interactive mode? 我们可以在python交互模式下做同样的事情吗?

In IPython running %matplotlib enables matplotlib interactive support without importing anything into the interactive namespace. 在IPython中运行%matplotlib可启用matplotlib交互式支持,而无需将任何内容导入交互式命名空间。 Then running plt.figure() immediately opens a new plot window, plt.imshow() populates it with an image and plt.plot() does the same for tabular data, all without blocking console interactivity. 然后运行plt.figure()会立即打开一个新的绘图窗口,plt.imshow()用图像填充它,plt.plot()对表格数据执行相同操作,所有这些都不会阻止控制台交互。

With matplotlib interactive mode enabled its ok to run plt.show() and it won't block anything even without setting block=False, which causes IPython to hang fatally in noninteractive mode. 使用matplotlib交互模式启用它可以运行plt.show(),即使没有设置block = False也不会阻塞任何内容,这会导致IPython在非交互模式下挂起致命。

%pylab also enables matplotlib interactive support, but it loads too many imports and is not recommended. %pylab还支持matplotlib交互式支持,但它加载了太多导入,不推荐使用。

External scripts executed with ' %run -i' run in IPython's namespace and will have interactive plotting support if its been enabled there. 使用' %run -i'执行的外部脚本在IPython的命名空间中运行,并且如果已在其中启用,则将具有交互式绘图支持。

For more information on matplotlib interactive mode see What is interactive mode? 有关matplotlib交互模式的更多信息,请参阅什么是交互模式? and Plotting with mathplotlib in IPython . 在IPython中用mathplotlib绘图

您需要在调用plt.ion()之前调用plt.show()来打开交互模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM