0

I am using a post processing plot script written in python, using python 3.7 and OSX 10.14

The script is probably not optimized, but it works good enough when I use my external monitors. When I try to run it on my macbook pro 15" monitor (2018), it freezes and becomes extremely slow and unresponsive unless I set the figure dimension small enough.

I have no idea of the reason, and I haven't found any explanation browsing the internet. Any clue on what can cause this behaviour and how I can fix it?

This is the 'cut' version of the script I'm running.

import numpy as np
import matplotlib
from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle, Circle
import sys


def main(iter0,iterN,its,out_dir='output'):
    it0 = int(iter0)
    itN = int(iterN)
    itstep = int(its)
    a = list(range(it0,itN+1,itstep))
    loaded_steps=len(a)
    e = np.loadtxt(out_dir+'/energy_data_dynamics')
    #... load many other files

    plt.ion()
    fig = plt.figure(figsize=(20, 20),facecolor="white")  #THESE DIMENSIONS WORK FOR ANY EXTERNAL SCREEN 
#    fig = plt.figure(figsize=(10, 10),facecolor="white") #THESE DIMENSIONS WORK FOR MACBOOK SCREEN

    it = 0
    for index in range(0,loaded_steps):
        plt.cla()
        plt.axes().set_aspect('equal')
        #... plot many other subplots
        ax414=plt.subplot(414)
        ax414.plot(e[it0+it,0], e[it0+it,2], marker="o", color="crimson", ms=15)
        ax414.plot(e[it0:itN+1,0], e[it0:itN+1,2],'-r')
        ax414.set_title("Energy")

        plt.show()
        plt.pause(0.0000000001)
        
        it+=itstep
        if index == loaded_steps-1:
            input("Plot finished - Press [enter] to continue.")




if __name__ == '__main__':
    matplotlib.use("TkAgg")
    its = 1
    out_dir = 'output'
    if len(sys.argv)>=4:
        its = sys.argv[3]
    if len(sys.argv)>=5:
        out_dir = sys.argv[4]
    main(sys.argv[1],sys.argv[2],its,out_dir)
Aethyr
  • 1
  • 2
  • 1
    tkAgg seems to have significant issues on MacOS - perhaps try using pyqt5 or the macOSx backend. You may also reconsider using a figure size that is so gigantic. 20 inches x 20 inches is bigger than most monitors, unless you have a 32 inch display... – Jody Klymak Jun 21 '21 at 17:31
  • The idea behind the 20x20 is that it auto-resize to max_dimension on most monitors, or at least that's the behaviour I have observed with tkAgg. Using macOSX seems to solve the "slowness", but then I can not change the dimensions anymore and the auto-resize does not work. Trying pyqt5 results in `ValueError: 'pyqt5' is not a valid value for backend; supported values are ['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']` – Aethyr Jun 21 '21 at 18:38
  • If you really want to do this you might try to maximize the window? https://stackoverflow.com/questions/12439588/how-to-maximize-a-plt-show-window-using-python – Jody Klymak Jun 21 '21 at 18:44
  • @JodyKlymak I saw that page but there is no proposed solution for macOSX backend, and it doesn't seem like I am able to use qt or wx backends... (maybe I am doing something wrong when trying to implement them?) edit: the use of macOSX isn't feasible: it stops all the interactions with the figure, so I can't zoom or save it, making the plot almost useless. – Aethyr Jun 21 '21 at 21:04
  • Try qt5agg. Sorry for being confusing – Jody Klymak Jun 22 '21 at 03:52
  • @JodyKlymak oh ok, thanks! qt5agg works on both screens. It skips some frames, but that's not the end of the world and I can definitely live with it. (but if you have a fast solution for this problem I'd love to implement it) – Aethyr Jun 22 '21 at 09:42

1 Answers1

0

It is hard to give a direct answer without running your code but the symptoms you are describing (works on an external monitor but not the internal one) sounds like you may be running into a GPU bug. External monitors may use a different GPU (usually the second GPU vs the built-in GPU). But the fact this simple script shows that slowdown implies there is a bug (your code, the driver, somewhere) or the function calls are causing a problem.

The figures seem tiny compared to what your are plotting: a figure size of 10x10 with a marker size of 15? I would start commenting out lines one by one using the built-in monitor until it gets fast again. There may also be a more efficient way to clear the axes and replot -- I've found Python / plt is very slow when I try to clear a figure and redraw. You may also want to try creating the subplot outside of your loop since some implementations/OSs have a lot of overhead for creating subplots.

Sean Walker
  • 108
  • 1
  • 9
  • If it's a GPU problem, do you have any idea of how to force the usage of a specific GPU? or any other suggestion? About the rest, I think sizes aren't really related, the figure is huge (20x20 is larger than the screen), the subplot fits and the marker is there as a decoration. Do you think I can upload a 'energy_data_dynamics' file for others to test the code? – Aethyr Jun 21 '21 at 20:59