2012-08-28 3 views
7

Ich versuche eine interaktive Handlung zu erstellen. Dieser sollte die Figur löschen, wenn innerhalb von Achsen geklickt wird und einen Kreis an einer zufälligen Stelle zeichnet. Der Code lautet wie folgt:Wie wird der maximale Rekursionstiefehler beim interaktiven Zeichnen entfernt?

import matplotlib.pyplot as plt 
import random 


def draw_circle(event): 
    if event.inaxes: 
     print(event.xdata, event.ydata) 
     plt.cla() 
     a = random.randint(0,100) 
     b = random.randint(0,100) 
     s, = plt.plot(a,b,'o', ms=100, color="blue",visible=True) 
     plt.show() 


fig = plt.figure() 
ax = plt.subplot(111) 
s, = plt.plot(1,2,'o', ms=100, color="blue",visible=True) 
plt.connect("button_press_event", draw_circle) 
plt.show() 

Nach 42-mal klicken, den Programmpausen und ich erhalte die folgenden Zurückverfolgungs:

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__ 
    return self.func(*args) 
    File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 286, in button_press_event 
    FigureCanvasBase.button_press_event(self, x, y, num, guiEvent=event) 
    File "/usr/lib/pymodules/python2.7/matplotlib/backend_bases.py", line 1632, in button_press_event 
    self.callbacks.process(s, mouseevent) 
    File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 262, in process 
    proxy(*args, **kwargs) 
    File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 192, in __call__ 
    return mtd(*args, **kwargs) 
    File "/home/almarahat/Dropbox/python/GUI/Testing site/test_rt/baud_test.py", line 8, in draw_circle 
    plt.cla() 
    File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2784, in cla 
    ret = gca().cla() 
    File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 842, in cla 
    spine.cla() 
    File "/usr/lib/pymodules/python2.7/matplotlib/spines.py", line 157, in cla 
    self.axis.cla() 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 731, in cla 
    self.reset_ticks() 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 744, in reset_ticks 
    self.majorTicks.extend([self._get_tick(major=True)]) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1553, in _get_tick 
    return XTick(self.axes, 0, '', major=major, **tick_kw) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 141, in __init__ 
    self.tick2line = self._get_tick2line() 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 401, in _get_tick2line 
    l.set_transform(self.axes.get_xaxis_transform(which='tick2')) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 586, in get_xaxis_transform 
    return self.spines['top'].get_spine_transform() 
    File "/usr/lib/pymodules/python2.7/matplotlib/spines.py", line 374, in get_spine_transform 
    self._ensure_position_is_set() 
    File "/usr/lib/pymodules/python2.7/matplotlib/spines.py", line 140, in _ensure_position_is_set 
    self.set_position(self._position) 
    File "/usr/lib/pymodules/python2.7/matplotlib/spines.py", line 365, in set_position 
    self.axis.cla() 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 731, in cla 
    self.reset_ticks() 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 744, in reset_ticks 
    self.majorTicks.extend([self._get_tick(major=True)]) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 1553, in _get_tick 
    return XTick(self.axes, 0, '', major=major, **tick_kw) 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 140, in __init__ 
    self.tick1line = self._get_tick1line() 
    File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 383, in _get_tick1line 
    zorder=self._zorder, 
    File "/usr/lib/pymodules/python2.7/matplotlib/lines.py", line 195, in __init__ 
    self._marker = MarkerStyle() 
    File "/usr/lib/pymodules/python2.7/matplotlib/markers.py", line 112, in __init__ 
    self.set_marker(marker) 
    File "/usr/lib/pymodules/python2.7/matplotlib/markers.py", line 171, in set_marker 
    self._recache() 
    File "/usr/lib/pymodules/python2.7/matplotlib/markers.py", line 116, in _recache 
    self._path = Path(np.empty((0,2))) 
    File "/usr/lib/pymodules/python2.7/matplotlib/path.py", line 112, in __init__ 
    if ma.isMaskedArray(vertices): 
    File "/usr/local/lib/python2.7/dist-packages/numpy-1.6.2-py2.7-linux-x86_64.egg/numpy/ma/core.py", line 5683, in isMaskedArray 
    return isinstance(x, MaskedArray) 
RuntimeError: maximum recursion depth exceeded while calling a Python object 

An diesem Punkt, ich bin nicht sicher, wo die Rekursion auftritt und wie um diesen Fehler zu beheben.

Ich verstehe (von anderen Q & As), dass ich meine Stack-Grenze erweitern und das Problem umgehen könnte. Ich halte dies jedoch nicht für eine Lösung in diesem speziellen Fall und möchte dem auf den Grund gehen.

Jede Hilfe wäre zu schätzen.

Vielen Dank im Voraus.

Weitere Informationen:

  • Die wichtigsten Funktionen, die Angelegenheiten umfassen die Figur Clearing und etwas Neue Zeichnung auf die Leinwand klicken.
  • Das Klicken erhöht diesen Fehler nicht, wenn ich nicht versuche, etwas zu plotten. Also, ich vermute, ich vermisse etwas im Umgang mit Matplotlib.

Antwort

7

Anstelle von plt.show(), aus Ihrem Rückruf plt.draw(). Das Problem ist, dass plt.show eine Hauptschleife der GUI-Bibliothek ausführt; Sie möchten nur das aktualisieren, was in der vorhandenen Hauptschleife angezeigt wird. Mit dem Qt-Backend würde Ihr Code den Fehler QCoreApplication::exec: The event loop is already running anzeigen.

Weitere Informationen zur interaktiven Verwendung von Matplotlib finden Sie unter What is interactive mode?.

+0

Brilliant ... Prost. –

Verwandte Themen