python画图显示不全屏_关于matplotlib:如何使用Python最大化plt.show()窗口

这篇博客介绍了如何在Python的matplotlib库中最大化图表窗口,特别是针对TkAgg、wxAgg和QT4Agg等不同后端的解决方案。作者提供了示例代码,包括使用`window.state('zoomed')`、`frame.Maximize(True)`和`showMaximized()`等方法,以适应Windows、Ubuntu等不同操作系统。同时,讨论了各种后端的兼容性和不同方法的效果,如全屏显示与最大化窗口的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出于好奇,我想在下面的代码中知道如何执行此操作。 我一直在寻找答案,但没有用。

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

Spoiler,在Windows上工作:先按plt.get_current_fig_manager().window.state(zoomed),再按plt.show()。

由于我的信誉为零,因此我只能留下新的答案

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

我能够使用以下几行来最大化TkAgg,QT4Agg和wxAgg的Figure窗口:

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

希望将以上答案的摘要(和一些补充内容)合并到一个有效的示例中(至少对于Windows是有用的)。

干杯

###可在Ubuntu上使用??? >>不适用于Windows mng.resize(* mng.window.maxsize())#对我来说在Linux上完美

@Daniele,您的解决方案在Ubuntu的TkAgg上对我有效。谢谢!但是花了我一段时间才能解析;)也许在" mng.resize ..."之前摆脱了一切。

有没有一种简单的方法来检查您正在使用的后端?现在有点使用试用结束错误。

不幸的是,当我输入figManager.window.showMaximized()时,我使用Qt5Agg尝试了您的代码,才弹出最大化的全屏窗口。下一行:plt.show()仅显示另一个窗口,该窗口在正常大小的窗口中绘制数据。

基于Tk的解决方案不适用于我:_tkinter.TclError: bad argument"zoomed": must be normal, iconic, or withdrawn(Ubuntu 16.04)。

在我的Ubuntu(16.04)计算机上,使用mng = plt.get_current_fig_manager() mng.full_screen_toggle()

Qt4Agg解决方案适用于我与Cytwin一起使用的Qt5。

使用Qt后端(FigureManagerQT)时,正确的命令是:

figManager = plt.get_current_fig_manager()

figManager.window.showMaximized()

之后仍然需要plt.show()。很好的答案,但可以在Windows上使用!

它适用于子图,而无需使用plt.show()。

_tkinter.tkapp对象具有bi属性showMaximized。总是更加相信Python比语言更是一个玩笑

@ HAL9000首先,这用于Qt4,而不是Tk。其次,您将语言归咎于外部包装设计问题。您可以使用任何语言来解决此类问题。

这对Cygwin Qt5来说对我有用。

在Ubuntu 12.04下,带有TkAgg后端的窗口将全屏显示给我:

mng = plt.get_current_fig_manager()

mng.resize(*mng.window.maxsize())

请注意,这对多台显示器设置有怪异的影响。该窗口将用尽所有监视器,而不是将其最大化。

这不会创建最大化的窗口(应该捕捉到屏幕的边缘),但是会创建一个非最大化的窗口,其大小为最大化的窗口。

这也成功地最大化了Ubuntu 14.04中的窗口,使顶部栏保持众所周知的按钮。

适用于Ubuntu 16.04和linux mint。 python2.7测试

对我来说,以上所有方法均无效。我在包含matplotlib 1.3.1的Ubuntu 14.04上使用Tk后端。

以下代码创建了一个全屏绘图窗口,该窗口与最大化窗口不同,但是很好地满足了我的目的:

from matplotlib import pyplot as plt

mng = plt.get_current_fig_manager()

mng.full_screen_toggle()

plt.show()

这也是对我有用的解决方案(尽管它可以全屏显示,而不是最大化窗口)。在Redhat Enterprise Linux 6,python 2.7.10,matplotlib 1.4.3上运行。

在Windows 10 x64和python 3.5的Visual Studio 2015中为我工作,但我无法访问窗口边框以关闭图形,因为它位于顶部屏幕像素上方。

对我来说,这也不会创建最大化的窗口,而是全屏显示的窗口。我没有像普通窗口那样有任何最小化,最大化/还原和关闭按钮,我必须右键单击任务栏上的窗口才能将其关闭。

全屏显示,但不显示每个窗口具有的按钮。在Ubuntu 14.04上试用。

在Raspbian(jessie)上像魅力一样工作

这应该起作用(至少对于TkAgg):

wm = plt.get_current_fig_manager()

wm.window.state('zoomed')

(从上文和《使用Tkinter》中采用,是否有一种方法可以在不明显缩放窗口的情况下获得可用的屏幕大小?)

好极了!这对我有用;它会创建一个最大化的窗口,该窗口捕捉到屏幕的边缘,并具有最小化,最大化/还原向下和关闭按钮。

但是,您的意思是这适用于TkAgg,而不是TkApp,对吗?

好抓(可能是错字)! TkAgg是Tk的后端。

我通常使用

mng = plt.get_current_fig_manager()

mng.frame.Maximize(True)

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

编辑:

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

使用这个,我在Matplotlib 1.2.0中得到了mng.frame.Maximize(True) AttributeError: FigureManagerTkAgg instance has no attribute frame

以上适用于Windows,您是否适用于Mac?

不,我在Windows上。

它与后端wx一起使用,我已经相应地更新了帖子。您正在使用的Tk后端可能不支持此功能。您可以选择将matplotlib后端更改为wx吗?

Mac上的错误:mng.frame.Maximize(True)AttributeError:FigureManagerMac对象没有属性框

是否有已知的解决方案在MacOSX后端上执行此操作? FigureManagerMac似乎既没有属性window也没有属性frame。

我在Windows上有同样的问题

这有点hacky,可能不是便携式的,只有在您想要快速又肮脏的情况下才使用它。如果我只是将图形设置为比屏幕大得多,那么它将占用整个屏幕。

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

实际上,在带有Qt4Agg的Ubuntu 16.04中,如果窗口大于屏幕,它将最大化窗口(而非全屏)。 (如果您有两个监视器,则仅将其中一个监视器最大化)。

为我工作!由于我试图获取屏幕的大小。

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

然后我查看了mng具有的属性,发现了这一点:

mng.window.showMaximized()

那对我有用。

因此,对于遇到同样麻烦的人,您可以尝试一下。

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

谢谢!该解决方案对我来说效果很好。在Redhat Enterprise Linux 6,python 2.7.10,matplotlib 1.4.3上运行。

我知道这会弹出一个全屏窗口,但是当我键入plt.show()时,我的绘图将出现在单独的窗口中。没有在这个全屏窗口上显示任何建议吗?

它也可以在Debian上的python 3.6和Qt后端上运行。

这无法在带有python 3.7的Windows 10 64bit上运行

我在Ubuntu上以全屏模式找到了它

#Show full screen

mng = plt.get_current_fig_manager()

mng.full_screen_toggle()

在我的版本(Python 3.6,Eclipse,Windows 7)中,上面给出的摘录不起作用,但是在Eclipse / pydev提示(键入:mng。)之后,我发现:

mng.full_screen_toggle()

看来使用mng-commands仅适用于本地开发...

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

尝试将" Figure.set_size_inches"方法与额外的关键字参数forward=True一起使用。根据文档,这应该调整图形窗口的大小。

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

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

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

高温超导

这就解释了我不小心按下了哪个键,全屏显示了我的窗户! (以及如何撤消它。)

好的,这对我有用。我做了整个showMaximize()选项,它确实根据图形的大小按比例调整了窗口的大小,但是它不会扩展和"适应"画布。我通过以下方法解决了这个问题:

mng = plt.get_current_fig_manager()

mng.window.showMaximized()

plt.tight_layout()

plt.savefig('Images/SAVES_PIC_AS_PDF.pdf')

plt.show()

一种完美地适用于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() =="win32":

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)

以下可能适用于所有后端,但是我仅在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()

这是一个基于@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()

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值