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

如何使用Python最大化plt.show()窗口,python,matplotlib,Python,Matplotlib,出于好奇,我想知道如何在下面的代码中做到这一点。我一直在寻找答案,但毫无用处 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(dat

出于好奇,我想知道如何在下面的代码中做到这一点。我一直在寻找答案,但毫无用处

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()

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

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

除此之外,要真正实现最大化,您将需要使用GUI工具箱特定的命令(如果您的特定后端存在这些命令)


HTH

尝试使用“Figure.set\u size\u inches”方法,使用额外的关键字参数
forward=True
。根据,这应该调整图形窗口的大小

这是否会发生取决于您使用的操作系统。

我通常使用

mng = plt.get_current_fig_manager()
mng.frame.Maximize(True)
在调用
plt.show()
之前,我得到一个最大化的窗口。这仅适用于“wx”后端

编辑:


有关Qt4Agg后端,请参阅kwerenda的。

这使我在Ubuntu12.04下的TkAgg后端窗口占据了整个屏幕:

    mng = plt.get_current_fig_manager()
    mng.resize(*mng.window.maxsize())
对于Qt后端(FigureManager Qt),正确的命令是:

figManager = plt.get_current_fig_manager()
figManager.window.showMaximized()
这应该起作用(至少与TkAgg一起):


(从上面和中采用)

我得到
mng.frame.Maximize(True)AttributeError:FigureManager实例也没有属性“frame”

然后我查看了属性
mng
has,发现:

mng.window.showMaximized()
这对我很管用

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

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

我在Windows(WIN7)上,运行Python 2.7.5和Matplotlib 1.3.1

我能够使用以下行最大化TkAgg、QT4Agg和wxAgg的图形窗口:

从matplotlib导入pyplot作为plt
###对于“TkAgg”后端
plt.图(1)
plt.switch_backend('TkAgg')35; TkAgg(代替Qt4Agg)
打印'#1后端:',plt.get#u后端()
plt.绘图([1,2,6,4])
mng=plt.get\u current\u fig\u manager()

###在Ubuntu上工作???>>没有在windows上工作 #mng.resize(*mng.window.maxsize()) mng.window.state('zoomed')#在Windows上运行良好! plt.show()#关闭图形以运行下一部分 ###对于“wxAgg”后端 plt.图(2) plt.switch_后端('wxAgg') 打印'#2后端:',plt.get#u后端() plt.绘图([1,2,6,4]) mng=plt.get\u current\u fig\u manager() mng.frame.Maximize(真) plt.show()#关闭图形以运行下一部分 ###对于“Qt4Agg”后端 plt.图(3) plt.switch_backend('QT4Agg')#我的系统上的默认值 打印'#3后端:',plt.get#u后端() plt.绘图([1,2,6,4]) figManager=plt.get_current_fig_manager() figManager.window.showMaximized() plt.show()
如果要最大化多个图形,可以使用

for fig in figs:
    mng = fig.canvas.manager
    # ...
希望前面答案的总结(以及一些补充)结合在一个工作示例中(至少对于windows)有所帮助。
干杯

对我来说,以上这些都不起作用。我使用Ubuntu14.04上的Tk后端,它包含matplotlib 1.3.1

下面的代码创建了一个全屏打印窗口,它与最大化不同,但很好地满足了我的目的:

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

这不一定会使窗口最大化,但它会根据图形的大小按比例调整窗口的大小:

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

这可能也有帮助:

以下内容可能适用于所有后端,但我仅在QT上测试过:

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()选项,它会根据图形的大小按比例调整窗口大小,但它不会展开并“适合”画布。我通过以下方式解决了这个问题:

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

plt.show()

这是一种黑客和可能不是便携式的,只有当你寻找快速和肮脏的使用它。如果我只是将数字设置为比屏幕大得多,它将完全占据整个屏幕

fig = figure(figsize=(80, 60))
事实上,在使用Qt4Agg的Ubuntu16.04中,如果窗口比屏幕大,它会最大化窗口(不是全屏)。(如果您有两个监视器,它只会在其中一个监视器上最大化它)。

在我的版本(Python 3.6、Eclipse、Windows 7)中,上面给出的代码片段不起作用,但在Eclipse/pydev给出的提示下(键入:mng之后),我发现:

mng.full_screen_toggle()

似乎使用mng命令只适用于本地开发…

这是一个完美解决Win 10问题的解决方案

import matplotlib.pyplot as plt

plt.plot(x_data, y_data)

mng = plt.get_current_fig_manager()
mng.window.state("zoomed")
plt.show()

到目前为止,我尽了最大努力,支持不同的后端:

from platform import system
def plt_maximize():
    # See discussion: https://stackoverflow.com/questions/12439588/how-to-maximize-a-plt-show-window-using-python
    backend = plt.get_backend()
    cfm = plt.get_current_fig_manager()
    if backend == "wxAgg":
        cfm.frame.Maximize(True)
    elif backend == "TkAgg":
        if system() == "Windows":
            cfm.window.state("zoomed")  # This is windows only
        else:
            cfm.resize(*cfm.window.maxsize())
    elif backend == "QT4Agg":
        cfm.window.showMaximized()
    elif callable(getattr(cfm, "full_screen_toggle", None)):
        if not getattr(cfm, "flag_is_max", None):
            cfm.full_screen_toggle()
            cfm.flag_is_max = True
    else:
        raise RuntimeError("plt_maximize() is not implemented for current backend:", backend)

我在Ubuntu上找到了全屏模式

#Show full screen
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()

下面是一个基于@Pythonio答案的函数。我将它封装到一个函数中,该函数自动检测它使用的是哪个后端,并执行相应的操作

def plt_set_fullscreen():
    backend = str(plt.get_backend())
    mgr = plt.get_current_fig_manager()
    if backend == 'TkAgg':
        if os.name == 'nt':
            mgr.window.state('zoomed')
        else:
            mgr.resize(*mgr.window.maxsize())
    elif backend == 'wxAgg':
        mgr.frame.Maximize(True)
    elif backend == 'Qt4Agg':
        mgr.window.showMaximized()

然后在
plt.show()之前调用函数
maximize()

对于后端GTK3Agg,请使用
maximize()
——尤其是小写字母m:

在Ubuntu20.04中使用Python 3.8进行测试。

对于基于Tk的后端(TkAgg),这两个选项最大化并全屏显示窗口:

plt.get_current_fig_manager().window.state('zoomed')
plt.get_current_fig_manager().window.attributes('-fullscreen', True)
打印到多个窗口时,需要为每个窗口编写以下内容:

data = rasterio.open(filepath)

blue, green, red, nir = data.read()
plt.figure(1)
plt.subplot(121); plt.imshow(blue);
plt.subplot(122); plt.imshow(red);
plt.get_current_fig_manager().window.state('zoomed')

rgb = np.dstack((red, green, blue))
nrg = np.dstack((nir, red, green))
plt.figure(2)
plt.subplot(121); plt.imshow(rgb);
plt.subplot(122); plt.imshow(nrg);
plt.get_current_fig_manager().window.state('zoomed')

plt.show()
在这里,两个“图形”都在单独的窗口中绘制。使用变量,例如

figure_manager = plt.get_current_fig_manager()

可能不会最大化第二个窗口,因为变量仍然引用第一个窗口。

使用此变量,我得到
mng.frame.maximize(True)AttributeError:FigureManager实例在Matplotlib 1.2.0中没有属性“frame”
,它与后端wx一起工作,我相应地更新了帖子。您正在使用的Tk后端可能不支持此功能。您是否可以选择将matplotlib后端更改为“wx”?mac上的错误:mng.frame.Maximize(True)AttributeError:“FigureManager mac”对象没有属性“frame”
MacOSX
后端是否有已知的解决方案?
FigureManager Mac
似乎既没有属性
window
也没有属性
frame
。我在Windows上也有同样的问题请注意,这对多监视器设置有奇怪的影响。窗口将用尽所有监视器,而不是最大化。这不会创建最大化窗口(该窗口应捕捉到
plt.get_current_fig_manager().window.state('zoomed')
plt.get_current_fig_manager().window.attributes('-fullscreen', True)
data = rasterio.open(filepath)

blue, green, red, nir = data.read()
plt.figure(1)
plt.subplot(121); plt.imshow(blue);
plt.subplot(122); plt.imshow(red);
plt.get_current_fig_manager().window.state('zoomed')

rgb = np.dstack((red, green, blue))
nrg = np.dstack((nir, red, green))
plt.figure(2)
plt.subplot(121); plt.imshow(rgb);
plt.subplot(122); plt.imshow(nrg);
plt.get_current_fig_manager().window.state('zoomed')

plt.show()
figure_manager = plt.get_current_fig_manager()