Matplotlib - Wymuś wyświetlanie wykresu, a następnie powróć do głównego kodu

To jest MWE tego, po czym jestem zaadaptowanyto pytanie:

from matplotlib.pyplot import plot, draw, show

def make_plot():
    plot([1,2,3])
    draw()
    print 'continue computation'

print('Do something before plotting.')
# Now display plot in a window
make_plot()

answer = raw_input('Back to main and window visible? ')
if answer == 'y':
    print('Excellent')
else:
    print('Nope')

show()

Chcę: wywołam funkcję, aby utworzyć wykres, pojawi się okno fabuły, a następnie powrócę do zachęty, abym mógł wprowadzić wartość (na podstawie tego obrazu, który właśnie się wyświetlił) i kontynuować kod (okno może się wtedy zamknąć lub pozostać tam, nie obchodzi mnie to).

Zamiast tego pojawia się okno z fabułąpo kod jest ukończony, co nie jest dobre.

Dodaj 1

Wypróbowałem poniższe z tymi samymi wynikami, okno fabuły pojawia się na końcu kodu, a nie przed:

from matplotlib.pyplot import plot, ion, draw

ion() # enables interactive mode
plot([1,2,3]) # result shows immediately (implicit draw())
# at the end call show to ensure window won't close.
draw()

answer = raw_input('Back to main and window visible? ')
if answer == 'y':
    print('Excellent')
else:
    print('Nope')

To samo dzieje się, jeśli się zmieniędraw() dlashow().

Dodaj 2

Próbowałem następującego podejścia:

from multiprocessing import Process
from matplotlib.pyplot import plot, show

def plot_graph(*args):
    for data in args:
        plot(data)
    show()

p = Process(target=plot_graph, args=([1, 2, 3],))
p.start()

print 'computation continues...'

print 'Now lets wait for the graph be closed to continue...:'
p.join()

co powodujePython kernel has crashed bład wCanopy z wiadomością:

The kernel (user Python environment) has terminated with error code -6. This may be due to a bug in your code or in the kernel itself.

Output captured from the kernel process is shown below.

[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing /tmp/tmp9cshhw.json
QGtkStyle could not resolve GTK. Make sure you have installed the proper libraries.
[xcb] Unknown sequence number while processing queue
[xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
[xcb] Aborting, sorry about that.
python: ../../src/xcb_io.c:274: poll_for_event: La declaración `!xcb_xlib_threads_sequence_lost' no se cumple.

Powinienem wspomnieć, że biegnęCanopy welementary OS który jest oparty naUbuntu 12.04.

Dodaj 3

Wypróbowano również rozwiązanie opublikowane wto pytanie:

import numpy
from matplotlib import pyplot as plt

if __name__ == '__main__':
    x = [1, 2, 3]
    plt.ion() # turn on interactive mode
    for loop in range(0,3):
        y = numpy.dot(x, loop)
        plt.figure()
        plt.plot(x,y)
        plt.show()
        _ = raw_input("Press [enter] to continue.")

Wyświetla puste okna wydruku w miarę przesuwania kodu (tj. Użytkownik uderza [enter]) i wyświetla tylko obrazy po zakończeniu kodu.

To rozwiązanie (także w tym samym pytaniu) nie wyświetla nawet okna wydruku:

import numpy
from matplotlib import pyplot as plt
if __name__ == '__main__':
    x = [1, 2, 3]
    plt.ion() # turn on interactive mode, non-blocking `show`
    for loop in range(0,3):
        y = numpy.dot(x, loop)
        plt.figure()   # create a new figure
        plt.plot(x,y)  # plot the figure
        plt.show()     # show the figure, non-blocking
        _ = raw_input("Press [enter] to continue.") # wait for input from the user
        plt.close()    # close the figure to show the next one.

questionAnswers(3)

yourAnswerToTheQuestion