Search code examples
pythonmatplotlibplotenthought

Python on windows, open plot windows next to each other


I am using EPD-python2.7 on windows7. In my python program I end up creating 4-5 figures in separate plot windows. By default the plot windows get stacked on top of each other. Every time I have to drag and replace each of these windows away and distribute over the screen area.

(Q1) Is there any way to set it automatically to have plot windows created next to each other? As shown below in the attached image (it is the screenshot of my second external screen).

(Q2) I have a second (extra) screen, and ideally I would like to have the plot windows created next to each other the second screen, when every time I run my programenter image description here


Solution

  • You can choose the location of your plot but it is dependant on backend. To check this,

    import matplotlib
    matplotlib.get_backend()
    

    and then see this answer for various ways to adjust. For example, this works for me on linux with Qt4Agg,

    import matplotlib.pyplot as plt
    
    #Choose the correct dimensions for your first screen
    FMwidth = 1280
    FMheight = 1024
    
    #Choose the correct dimensions for your second screen
    SMwidth = 1280
    SMheight = 1024
    
    fm = plt.get_current_fig_manager()
    #Works with QT on linux 
    fm.window.setGeometry(FMwidth,0,SMwidth,SMheight)
    

    This may be better for windows

    fm.window.wm_geometry("+500+0")
    

    You may also be able to get the screen size(s) from,

    from win32api import GetSystemMetrics
    
    width = GetSystemMetrics(0)
    weight = GetSystemMetrics(1)
    

    You can easily create a counter which increments whenever you create a plot and adjusts this specified position so the next plot is next to the previous one each time. However, the size and layout are much easier if you use subplots, for example to set up your 2 by 3 grid,

    #Setup 2 by 3 grid of subplots
    fig, axs = plt.subplots(2,3,figsize=(width,height))
    
    axs[0,0].plot(x, np.sinc(x))
    axs[1,0].hist(y)
    etc
    

    You can then use your counter to specify which plot you are currently using and increment this whenever you plot.