如何使用Python最大化plt.show()窗口

只是为了好奇,我想知道如何在下面的代码中做到这一点。 我一直在寻找答案,但是没用。

import numpy as np import matplotlib.pyplot as plt data=np.random.exponential(scale=180, size=10000) print ('el valor medio de la distribucion exponencial es: ') print np.average(data) plt.hist(data,bins=len(data)**0.5,normed=True, cumulative=True, facecolor='red', label='datos tamano paqutes acumulativa', alpha=0.5) plt.legend() plt.xlabel('algo') plt.ylabel('algo') plt.grid() plt.show() 

我通常使用

 mng = plt.get_current_fig_manager() mng.frame.Maximize(True) 

在调用plt.show() ,我得到一个最大化的窗口。 这只适用于'wx'后端。

编辑:

对于Qt4Agg后端,请参阅kwerenda的答案 。

因为我在零信誉,我可以留下任何新的答案,我在Windows(WIN7),运行Python 2.7.5和Matplotlib 1.3.1

我能用TkAgg,QT4Agg和wxAgg最大化图窗口使用以下几行:

 from matplotlib import pyplot as plt ### for 'TkAgg' backend plt.figure(1) plt.switch_backend('TkAgg') #TkAgg (instead Qt4Agg) print '#1 Backend:',plt.get_backend() plt.plot([1,2,6,4]) mng = plt.get_current_fig_manager() ### works on Ubuntu??? >> did NOT working on windows # mng.resize(*mng.window.maxsize()) mng.window.state('zoomed') #works fine on Windows! plt.show() #close the figure to run the next section ### for 'wxAgg' backend plt.figure(2) plt.switch_backend('wxAgg') print '#2 Backend:',plt.get_backend() plt.plot([1,2,6,4]) mng = plt.get_current_fig_manager() mng.frame.Maximize(True) plt.show() #close the figure to run the next section ### for 'Qt4Agg' backend plt.figure(3) plt.switch_backend('QT4Agg') #default on my system print '#3 Backend:',plt.get_backend() plt.plot([1,2,6,4]) figManager = plt.get_current_fig_manager() figManager.window.showMaximized() plt.show() 

希望这个总结在以前的答案(和一些增加)结合在一个工作的例子(至less对于Windows)的帮助。 干杯

有了Qt后台(FigureManagerQT)正确的命令是:

 figManager = plt.get_current_fig_manager() figManager.window.showMaximized() 

这使得窗口在我的Ubuntu 12.04下与TkAgg后端占据了整个屏幕:

  mng = plt.get_current_fig_manager() mng.resize(*mng.window.maxsize()) 

对我来说,上述任何工作。 我在Ubuntu 14.04上使用了包含matplotlib 1.3.1的Tk后端。

下面的代码创build一个全屏幕的graphics窗口,这是不一样的最大化,但它很好地服务于我的目的:

 from matplotlib import pyplot as plt mng = plt.get_current_fig_manager() mng.full_screen_toggle() plt.show() 

这应该工作( 至less与TkAgg):

 wm = plt.get_current_fig_manager() wm.window.state('zoomed') 

(从上面采用和使用Tkinter,有没有一种方法来获得可用的屏幕大小,而不可见的缩放窗口? )

按住f键(或1.2rc1中的ctrl+f ),当聚焦在一个绘图上时,将全屏显示一个绘图窗口。 不是最大化,但可能更好。

除此之外,要实际最大化,您将需要使用GUI Toolkit特定命令(如果它们存在于您的特定后端)。

HTH

我得到mng.frame.Maximize(True) AttributeError: FigureManagerTkAgg instance has no attribute 'frame'

然后我查看了这个属性,我发现这个:

 mng.window.showMaximized() 

这对我有效。

所以对于有同样问题的人,你可以试试这个。

顺便说一下,我的Matplotlib版本是1.3.1。

尝试plt.figure(figsize=(6*3.13,4*3.13))以使图更大。

尝试使用“Figure.set_size_inches”方法,使用extra关键字参数forward=True 。 根据文档 ,这应该调整数字窗口。

实际上会发生什么取决于您正在使用的操作系统。

这是一种黑客行为,可能不便携式,只有在你寻找快速和肮脏的时候才使用它。 如果我只是把这个数字设置得比屏幕大得多,那么它就是整个屏幕。

 fig = figure(figsize=(80, 60)) 

事实上,在Qt4Agg的Ubuntu 16.04中,如果它大于屏幕,它将最大化窗口(不是全屏)。 (如果你有两台显示器,它只是在其中一台显示器上最大化)。

这不一定是最大化你的窗口,但它会调整你的窗口的大小成正比:

 from matplotlib import pyplot as plt F = gcf() Size = F.get_size_inches() F.set_size_inches(Size[0]*2, Size[1]*2, forward=True)#Set forward to True to resize window along with plot in figure. plt.show() #or plt.imshow(z_array) if using an animation, where z_array is a matrix or numpy array 

这也可能有所帮助: http : //matplotlib.1069221.n5.nabble.com/Resizing-figure-windows-td11424.html

以下可能适用于所有的后端,但我只在QT上testing过它:

 import numpy as np import matplotlib.pyplot as plt import time plt.switch_backend('QT4Agg') #default on my system print('Backend: {}'.format(plt.get_backend())) fig = plt.figure() ax = fig.add_axes([0,0, 1,1]) ax.axis([0,10, 0,10]) ax.plot(5, 5, 'ro') mng = plt._pylab_helpers.Gcf.figs.get(fig.number, None) mng.window.showMaximized() #maximize the figure time.sleep(3) mng.window.showMinimized() #minimize the figure time.sleep(3) mng.window.showNormal() #normal figure time.sleep(3) mng.window.hide() #hide the figure time.sleep(3) fig.show() #show the previously hidden figure ax.plot(6,6, 'bo') #just to check that everything is ok plt.show() 

好的,这是我的工作。 我做了整个showMaximize()选项,它会根据graphics的大小调整窗口的大小,但不会展开和“合适”canvas。 我解决了这个问题:

 mng = plt.get_current_fig_manager() mng.window.showMaximized() plt.tight_layout() plt.savefig('Images/SAVES_PIC_AS_PDF.pdf') plt.show()