def pause_debugplot(debugplot, optional_prompt=None, pltshow=False,
                    tight_layout=True):
    if debugplot not in DEBUGPLOT_CODES:
        raise ValueError('Invalid debugplot value:', debugplot)
    if debugplot < 0:
        debugplot_ = -debugplot
        pltclose = True
    else:
        debugplot_ = debugplot
        pltclose = False
    if pltshow:
        if debugplot_ in [1, 2, 11, 12, 21, 22]:
            if tight_layout:
                plt.tight_layout()
            if debugplot_ in [1, 11, 21]:
                plt.show(block=False)
                plt.pause(0.2)
            elif debugplot_ in [2, 12, 22]:
                print('Press "q" to continue...', end='')
                sys.stdout.flush()
                plt.show()
                print('')
    else:
        if debugplot_ in [2, 12, 22]:
            if optional_prompt is None:
                print('Press <RETURN> to continue...', end='')
            else:
                print(optional_prompt, end='')
            sys.stdout.flush()
            cdummy = sys.stdin.readline().strip()
    if debugplot_ in [1, 2, 11, 12, 21, 22] and pltclose:
        plt.close()