Matplotlib recursion error when showing consecutive plots

2

I have a list with roughly 3000 items (every item is a list of 10points). One item might look like this:

[['-19.05364812189936' '-8.078571934798015']
 ['-4.481956059532247' '10.88512402551382']
 ['-15.985506732813608' '-19.92397590361446']
 ['-1.2933416017009214' '-10.233128986534373']
 ['-19.640113394755492' '2.7974946846208364']
 ['-9.466119773210488' '1.8171420978029766']
 ['4.998052799433026' '-0.5782990786676117']
 ['-7.883010276399716' '-9.731805457122608']
 ['-2.754521615875266' '-18.244450744153085']
 ['-17.105912473423103' '13.09440644932672']
 ['right' '0']]

What I want in the end is to plot the first item, do some user interaction (assign a label to the plotted points), confirm (save to another list: 10 points + label) and plot the next and so on.

Here is my current code:

import numpy as np
import Utils as utils
import matplotlib.pyplot as plt

methods = utils.ClusteringMethods()
db = utils.DbConnection()

formations = methods.readFromDump('testNewInterval')

formation_list = []
formation_list.append('5-3-2')
formation_list.append('4-4-2')
formation_list.append('4-3-3')

labelled_formations = []

def add_formation_def():
    name = input('Please specify formation')
    if(name not in formation_list):
        formation_list.append(name)
    else:
        print('Already there, please try again')

def showOptionList():
    for idx, item in enumerate(formation_list):
        print(idx, '  -  ', item)

def getCurrentFormation(index):
    return formations[index]

def showClearPlot():
    ax1.clear()
    ax1.imshow(im, extent=[-55, 55, -35, 35], aspect='auto')

def nextPlot():
    print('in next plot..')
    global currentIndex
    global currentFormation
    currentIndex = currentIndex +1
    ax1.clear()
    ax1.imshow(im, extent=[-55, 55, -35, 35], aspect='auto')

    currentFormation = getCurrentFormation(currentIndex)

    showPlot(currentFormation)

def showPlot(currentFormation):
    orientation = currentFormation[-1][0]
    print('orientation: ', orientation)
    currentFormation = currentFormation[:-1]

    for point in currentFormation:
        ax1.plot(float(point[0]), float(point[1]), '.', markersize=14, mfc='darkgreen', mec='k')

    plt.show()


def on_key(event):
    print('key: ', event.key)

    if (event.key == ' '):
        showOptionList()

    if (event.key == 'f1'):
        add_formation_def()

    if(event.key == 'f2'):
        nextPlot()

#Initialize Pitch
fig = plt.figure(figsize=(11,6))
ax1 = fig.add_subplot(111)
im = plt.imread('field.png')
ax1.imshow(im, extent=[-55, 55, -35, 35], aspect='auto')

#register events
cid = fig.canvas.mpl_connect('key_press_event', on_key)

currentIndex = 0
currentFormation = formations[currentIndex]

showPlot(currentFormation)

It works for a few items but eventually I'm getting the following error:

RecursionError: maximum recursion depth exceeded while calling a Python object

EDIT: Full stacktrace:

Traceback (most recent call last):
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388, in process
    proxy(*args, **kwargs)
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228, in __call__
    return mtd(*args, **kwargs)
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 67, in on_key
    nextPlot()
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 40, in nextPlot
    ax1.clear()
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axes\_base.py", line 1116, in clear
    self.cla()
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axes\_base.py", line 982, in cla
    spine.cla()
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\spines.py", line 183, in cla
    self.axis.cla()
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 773, in cla
    self.reset_ticks()
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 787, in reset_ticks
    self.majorTicks.extend([self._get_tick(major=True)])
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 2097, in _get_tick
    return YTick(self.axes, 0, '', major=major, **tick_kw)
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 154, in __init__
    self.tick1line = self._get_tick1line()
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 569, in _get_tick1line
    l.set_transform(self.axes.get_yaxis_transform(which='tick1'))
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axes\_base.py", line 779, in get_yaxis_transform
    return self.spines['left'].get_spine_transform()
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\spines.py", line 442, in get_spine_transform
    self._ensure_position_is_set()
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\spines.py", line 165, in _ensure_position_is_set
    self.set_position(self._position)
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\spines.py", line 432, in set_position
    self.axis.reset_ticks()
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 787, in reset_ticks
    self.majorTicks.extend([self._get_tick(major=True)])
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 2097, in _get_tick
    return YTick(self.axes, 0, '', major=major, **tick_kw)
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 152, in __init__
    self.apply_tickdir(tickdir)
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 525, in apply_tickdir
    self.stale = True
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\artist.py", line 241, in stale
    self.stale_callback(self, val)
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\artist.py", line 68, in _stale_axes_callback
    self.axes.stale = val
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\artist.py", line 241, in stale
    self.stale_callback(self, val)
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\figure.py", line 56, in _stale_figure_callback
    self.figure.stale = val
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\artist.py", line 241, in stale
    self.stale_callback(self, val)
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\pyplot.py", line 582, in _auto_draw_if_interactive
    if val and matplotlib.is_interactive() and not fig.canvas.is_saving():
RecursionError: maximum recursion depth exceeded

Current thread 0x00003ab4 (most recent call first):
  File "C:\ProgramData\Anaconda3\lib\abc.py", line 182 in __instancecheck__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\markers.py", line 255 in set_marker
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\markers.py", line 189 in __init__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\lines.py", line 402 in __init__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 568 in _get_tick1line
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 154 in __init__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 2097 in _get_tick
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 787 in reset_ticks
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\spines.py", line 432 in set_position
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\spines.py", line 165 in _ensure_position_is_set
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\spines.py", line 442 in get_spine_transform
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axes\_base.py", line 779 in get_yaxis_transform
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 569 in _get_tick1line
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 154 in __init__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 2097 in _get_tick
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 787 in reset_ticks
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axis.py", line 773 in cla
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\spines.py", line 183 in cla
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axes\_base.py", line 982 in cla
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\axes\_base.py", line 1116 in clear
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 40 in nextPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 67 in on_key
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228 in __call__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388 in process
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 1880 in key_press_event
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 441 in key_press
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699 in __call__
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 557 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 1078 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 218 in show
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\pyplot.py", line 254 in show
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 55 in showPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 45 in nextPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 67 in on_key
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228 in __call__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388 in process
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 1880 in key_press_event
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 441 in key_press
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699 in __call__
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 557 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 1078 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 218 in show
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\pyplot.py", line 254 in show
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 55 in showPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 45 in nextPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 67 in on_key
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228 in __call__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388 in process
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 1880 in key_press_event
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 441 in key_press
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699 in __call__
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 557 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 1078 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 218 in show
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\pyplot.py", line 254 in show
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 55 in showPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 45 in nextPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 67 in on_key
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228 in __call__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388 in process
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 1880 in key_press_event
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 441 in key_press
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699 in __call__
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 557 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 1078 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 218 in show
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\pyplot.py", line 254 in show
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 55 in showPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 45 in nextPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 67 in on_key
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228 in __call__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388 in process
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 1880 in key_press_event
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 441 in key_press
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699 in __call__
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 557 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 1078 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 218 in show
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\pyplot.py", line 254 in show
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 55 in showPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 45 in nextPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 67 in on_key
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228 in __call__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388 in process
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 1880 in key_press_event
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 441 in key_press
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699 in __call__
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 557 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 1078 in mainloop
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 218 in show
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\pyplot.py", line 254 in show
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 55 in showPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 45 in nextPlot
  File "C:/Users/D071947/PycharmProjects/Formation/LabelFormations.py", line 67 in on_key
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 228 in __call__
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\cbook\__init__.py", line 388 in process
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backend_bases.py", line 1880 in key_press_event
  File "C:\Users\D071947\PycharmProjects\Formation\venv\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 441 in key_press
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699 in __call__
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 557 in mainloop
  ...

I guess its because of extensive use of

plt.show()

in showplot().

I also read a lot about using the interactive mode (ion()) and replacing .show() with .draw() which is not really working for me because it then doesn't show any plot.

python
matplotlib
asked on Stack Overflow Mar 21, 2018 by Christian • edited Mar 21, 2018 by Christian

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0