155

Just for curiosity I would like to know how to do this in the code below. I have been searching for an answer but is useless.

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()
3
  • 1
    Tip for Windows: the only that worked for me was plt.get_current_fig_manager().window.state('zoomed').
    – Basj
    Commented Sep 8, 2023 at 12:46
  • mng = plt.get_current_fig_manager() mng.window.maximize()
    – Sigur
    Commented Jan 3 at 19:55
  • plt.get_current_fig_manager().window.showMaximized() works
    – Kevin K
    Commented Mar 13 at 4:37

24 Answers 24

210

I am on a Windows (WIN7), running Python 2.7.5 & Matplotlib 1.3.1.

I was able to maximize Figure windows for TkAgg, QT4Agg, and wxAgg using the following lines:

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

if you want to maximize multiple figures you can use

for fig in figs:
    mng = fig.canvas.manager
    # ...

Hope this summary of the previous answers (and some additions) combined in a working example (at least for windows) helps.

9
  • 8
    ### works on Ubuntu??? >> did NOT working on windows mng.resize(*mng.window.maxsize()) #works perfect on linux for me
    – Daniele
    Commented Jun 7, 2015 at 16:47
  • 1
    @Daniele, your solution works for me on TkAgg on Ubuntu. Thanks! But it took me a while to parse ;) Maybe get rid of everything before "mng.resize...".
    – BenB
    Commented Oct 4, 2015 at 7:09
  • 1
    Is there an easy way to check what backend you are using? kinda used trial end error now. Commented Jun 8, 2016 at 21:00
  • 2
    Unfortunately, I tried your code with Qt5Agg, when I type in figManager.window.showMaximized(), the maximized fullscreen window just poped up. The next line: plt.show() just show another window which plots the data in a normal size window. Commented Apr 27, 2017 at 3:43
  • 4
    The Tk based solution does not work for me: _tkinter.TclError: bad argument "zoomed": must be normal, iconic, or withdrawn (Ubuntu 16.04).
    – bluenote10
    Commented Jul 3, 2018 at 12:22
107

With Qt backend (FigureManagerQT) proper command is:

figManager = plt.get_current_fig_manager()
figManager.window.showMaximized()
10
  • 3
    This still requires plt.show() afterwards. Great answer though, works on windows!
    – lucidbrot
    Commented Sep 24, 2018 at 7:39
  • 2
    '_tkinter.tkapp' object has bi attribute 'showMaximized'. Always more convinced that Python is a joke more than a language
    – HAL9000
    Commented Sep 11, 2019 at 15:46
  • 4
    @HAL9000 First, this is for Qt4, not Tk. Second, you're blaming a language for what is an external package design issue. You can have this kind of issue in any language. Commented Nov 25, 2019 at 14:46
  • 13
    I get AttributeError: '_tkinter.tkapp' object has no attribute 'showMaximized' on Windows.
    – Basj
    Commented Nov 9, 2020 at 11:48
  • 1
    Works with the PyQt5 backend for me.
    – Stefan
    Commented Jan 21, 2021 at 17:34
57

This makes the window take up the full screen for me, under Ubuntu 12.04 with the TkAgg backend:

    mng = plt.get_current_fig_manager()
    mng.resize(*mng.window.maxsize())
6
  • 11
    Note that this has weird effects on a multiple monitors setup. The window will use up all monitors, instead of being maximized. Commented Apr 17, 2013 at 14:08
  • 5
    This will not create a maximized window (which should snap to the edges of the screen), but create a non-maximized window with the size of a maximized one. Commented Feb 6, 2017 at 11:45
  • This successfully maximizes the window in Ubuntu 14.04 too, keeping the top bar with the buttons we all know.
    – Irene
    Commented Jun 12, 2017 at 13:25
  • Works on Ubuntu 16.04 and linux mint. python2.7 tested Commented Aug 11, 2018 at 22:04
  • @user1202136 Worked fine for me in a 3-monitor setup. Commented Jan 17, 2020 at 12:19
50

This should work (at least with TkAgg):

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

(adopted from the above and Using Tkinter, is there a way to get the usable screen size without visibly zooming a window?)

6
  • 3
    Yay! This worked for me; it creates a maximized window that snaps to the edges of the screen and has the minimize, maximize/restore down and close buttons as it should. Commented Feb 6, 2017 at 11:53
  • 1
    However, you mean that this works with TkAgg, not TkApp, right? Commented Feb 8, 2017 at 15:42
  • 4
    Just tested this for matplotlib 2 / python 3 . Works under windows! Commented Mar 29, 2020 at 16:43
  • 1
    Alas, does not work on Debian linux, which doesn't have the 'zoomed' state. Commented Apr 9, 2021 at 16:44
  • 1
    This is the solution that worked for me on Windows 11 with the default matplotlib installation.
    – Zciurus
    Commented Jan 12, 2023 at 11:06
49

For me nothing of the above worked. I use the Tk backend on Ubuntu 14.04 which contains matplotlib 1.3.1.

The following code creates a fullscreen plot window which is not the same as maximizing but it serves my purpose nicely:

from matplotlib import pyplot as plt
mng = plt.get_current_fig_manager()
mng.full_screen_toggle()
plt.show()
6
  • 3
    This was also the solution that worked for me (although it goes to full screen, not maximised window). Running on Redhat Enterprise Linux 6, python 2.7.10, matplotlib 1.4.3. Commented Sep 9, 2015 at 8:25
  • 1
    Worked for me within Visual Studio 2015 on Windows 10 x64, python 3.5 except that I could not access the window border to close the figure, as it was above the top screen pixels.
    – lightw8
    Commented Aug 20, 2016 at 0:26
  • 2
    For me, this doesn't create a maximized window either, but a fullscreen one. I didn't get any minimize, maximize/restore down and close buttons as normal windows have and I had to right-click on the window on the taskbar to be able to close it. Commented Feb 6, 2017 at 11:51
  • 3
    This goes full screen without showing the buttons that every window has. Tried on Ubuntu 14.04.
    – Irene
    Commented Jun 12, 2017 at 13:28
  • 1
    Working like a charm on Raspbian (jessie)
    – anatol
    Commented Jul 9, 2017 at 22:54
46

I usually use

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

before the call to plt.show(), and I get a maximized window. This works for the 'wx' backend only.

EDIT:

for Qt4Agg backend, see kwerenda's answer.

8
  • 80
    Using this, I get mng.frame.Maximize(True) AttributeError: FigureManagerTkAgg instance has no attribute 'frame' in Matplotlib 1.2.0
    – Junuxx
    Commented Dec 7, 2012 at 14:01
  • 2
    It works with backend wx, I've updated the post accordingly. Likely the Tk backend you are using doesn't support this feature. Do you have the option to change matplotlib backend to 'wx'?
    – gg349
    Commented Dec 10, 2012 at 7:45
  • 14
    error on mac: mng.frame.Maximize(True) AttributeError: 'FigureManagerMac' object has no attribute 'frame'
    – user391339
    Commented Jun 14, 2016 at 15:53
  • 10
    Is there a known solution to do this on the MacOSXbackend? The FigureManagerMac seems to have neither the attribute windownor frame.
    – McLawrence
    Commented Feb 18, 2018 at 18:23
  • 4
    I have the same issue on Windows
    – RollRoll
    Commented Nov 24, 2018 at 20:46
17

My best effort so far, supporting different backends:

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

I get mng.frame.Maximize(True) AttributeError: FigureManagerTkAgg instance has no attribute 'frame' as well.

Then I looked through the attributes mng has, and I found this:

mng.window.showMaximized()

That worked for me.

So for people who have the same trouble, you may try this.

By the way, my Matplotlib version is 1.3.1.

4
  • Thanks! This solution worked well for me. Running on Redhat Enterprise Linux 6, python 2.7.10, matplotlib 1.4.3. Commented Sep 9, 2015 at 8:31
  • I know this will pop up a fullscreen window, but my plots will come up in a separate window when I type plt.show(). Not on this fullscreen window, any suggestion? Commented Apr 27, 2017 at 3:45
  • It also works on python 3.6 on Debian and with Qt backend.
    – pbalaga
    Commented Jan 5, 2019 at 19:44
  • this does not run on windows 10 64bit with python 3.7
    – George Sp
    Commented Jan 26, 2019 at 15:04
11

I found this for full screen mode on Ubuntu

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

This is kind of hacky and probably not portable, only use it if you're looking for quick and dirty. If I just set the figure much bigger than the screen, it takes exactly the whole screen.

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

In fact, in Ubuntu 16.04 with Qt4Agg, it maximizes the window (not full-screen) if it's bigger than the screen. (If you have two monitors, it just maximizes it on one of them).

0
9

The one solution that worked on Win 10 flawlessly.

import matplotlib.pyplot as plt

plt.plot(x_data, y_data)

mng = plt.get_current_fig_manager()
mng.window.state("zoomed")
plt.show()
9
import matplotlib.pyplot as plt
def maximize():
    plot_backend = plt.get_backend()
    mng = plt.get_current_fig_manager()
    if plot_backend == 'TkAgg':
        mng.resize(*mng.window.maxsize())
    elif plot_backend == 'wxAgg':
        mng.frame.Maximize(True)
    elif plot_backend == 'Qt4Agg':
        mng.window.showMaximized()

Then call function maximize() before plt.show()

6

For backend GTK3Agg, use maximize() – notably with a lower case m:

manager = plt.get_current_fig_manager()
manager.window.maximize()

Tested in Ubuntu 20.04 with Python 3.8.

4

Here is a function based on @Pythonio's answer. I encapsulate it into a function that automatically detects which backend is it using and do the corresponding actions.

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

Pressing the f key (or ctrl+f in 1.2rc1) when focussed on a plot will fullscreen a plot window. Not quite maximising, but perhaps better.

Other than that, to actually maximize, you will need to use GUI Toolkit specific commands (if they exist for your specific backend).

HTH

1
  • This explains which key I kept accidentally hitting that fullscreened my windows! (And how to undo it.)
    – cxrodgers
    Commented Jul 6, 2015 at 18:28
2

In my versions (Python 3.6, Eclipse, Windows 7), snippets given above didn't work, but with hints given by Eclipse/pydev (after typing: mng.), I found:

mng.full_screen_toggle()

It seems that using mng-commands is ok only for local development...

2

Try using 'Figure.set_size_inches' method, with the extra keyword argument forward=True. According to the documentation, this should resize the figure window.

Whether that actually happens will depend on the operating system you are using.

1

Ok so this is what worked for me. I did the whole showMaximize() option and it does resize your window in proportion to the size of the figure, but it does not expand and 'fit' the canvas. I solved this by:

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

plt.show()
1

For Tk-based backend (TkAgg), these two options maximize & fullscreen the window:

plt.get_current_fig_manager().window.state('zoomed')
plt.get_current_fig_manager().window.attributes('-fullscreen', True)

When plotting into multiple windows, you need to write this for each window:

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

Here, both 'figures' are plotted in separate windows. Using a variable such as

figure_manager = plt.get_current_fig_manager()

might not maximize the second window, since the variable still refers to the first window.

1

I collected a few answers from the threads I was looking at when trying to achieve the same thing. This is the function I am using right now which maximizes all plots and doesn't really care about the backend being used. I run it at the end of the script. It does still run into the problem mentioned by others using multiscreen setups, in that fm.window.maxsize() will get the total screen size rather than just that of the current monitor. If you know the screensize you want them you can replace *fm.window.maxsize() with the tuple (width_inches, height_inches).

Functionally all this does is grab a list of figures, and resize them to matplotlibs current interpretation of the current maximum window size.

def maximizeAllFigures():
    '''
    Maximizes all matplotlib plots.
    '''
    for i in plt.get_fignums():
        plt.figure(i)
        fm = plt.get_current_fig_manager()
        fm.resize(*fm.window.maxsize())
0
1

I have tried most of above solutions but none of them works well on my Windows 10 with Python 3.10.5.

Below is what I found that works perfectly on my side.

import ctypes

mng = plt.get_current_fig_manager()
mng.resize(ctypes.windll.user32.GetSystemMetrics(0), ctypes.windll.user32.GetSystemMetrics(1))
0

Try plt.figure(figsize=(6*3.13,4*3.13)) to make the plot larger.

0

This doesn't necessarily maximize your window, but it does resize your window in proportion to the size of the figure:

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

This might also help: http://matplotlib.1069221.n5.nabble.com/Resizing-figure-windows-td11424.html

0

The following may work with all the backends, but I tested it only on 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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.