Skip to content

[Bug]: matplotlib basemap colorbar exception : Given element not contained in the stack #23186

@redstoneleo

Description

@redstoneleo

Bug summary

Traceback (most recent call last):
  File "C:\Users\Oscar\Documents\ConsumerSoftwareProject\Plot\pyqt��������.py", line 44, in handle_clicked_1
    self.update_plot(
  File "C:\Users\Oscar\Documents\ConsumerSoftwareProject\Plot\pyqt��������.py", line 109, in update_plot
    cb = m.colorbar()
  File "C:\Users\Oscar\AppData\Local\Programs\Python\Python39\lib\site-packages\mpl_toolkits\basemap\__init__.py", line 4647, in colorbar
    fig.sca(ax) # reset parent axes as current axes.
  File "C:\Users\Oscar\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\figure.py", line 1535, in sca
    self._axstack.bubble(a)
  File "C:\Users\Oscar\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\figure.py", line 82, in bubble
    return super().bubble(self._entry_from_axes(a))
  File "C:\Users\Oscar\AppData\Local\Programs\Python\Python39\lib\site-packages\matplotlib\cbook\__init__.py", line 638, in bubble
    raise ValueError('Given element not contained in the stack')
ValueError: Given element not contained in the stack

Code for reproduction

with the following **self-contained** code, so how to fix the problem?

    import sys
    
    from PyQt5 import QtWidgets
    
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
    from matplotlib.figure import Figure
    
    import numpy as np
    
    from mpl_toolkits.basemap import Basemap
    from matplotlib import cm
    
    
    class MainWindow(QtWidgets.QMainWindow):
            def __init__(self, parent=None):
                    super().__init__(parent)
    
                    button1 = QtWidgets.QPushButton("button1")
                    button2 = QtWidgets.QPushButton("button2")
    
                    self.map_canvas = FigureCanvas(Figure(figsize=(20, 20)))
                    self.map_toolbar = NavigationToolbar(self.map_canvas, self)
    
                    central_widget = QtWidgets.QWidget()
                    self.setCentralWidget(central_widget)
                    lay = QtWidgets.QGridLayout(central_widget)
                    lay.addWidget(button1, 0, 0)
                    lay.addWidget(button2, 0, 1)
                    lay.addWidget(self.map_canvas, 1, 0, 1, 2)
    
                    self.addToolBar(self.map_toolbar)
    
                    button1.clicked.connect(self.update_plot)
                    button2.clicked.connect(self.update_plot)
    
                    self.ax = self.map_canvas.figure.subplots()
                    self.ax.set_axis_off()
                    # help(self.map_canvas.figure)
    
            def update_plot(self):
                    self.ax.clear()
    
                    m = Basemap(width=12000000, height=9000000, projection='lcc', resolution='c', lat_0=19., lon_0=73., ax=self.ax,)
                    m.drawcoastlines(linewidth=0.25)  # I added color='red', lw=2.0
    
                    x, y, z = np.random.rand(3, 1000000)
                    x *= 12e6
                    y *= 9e6
                    z *= 20000
    
                    gridsize = 100
                    m.hexbin(x, y, C=z, gridsize=gridsize, cmap=cm.YlGnBu)
    
                    cb = m.colorbar()
    
                    # self.map_canvas.figure.title('SAT on January 1')
                    # self.map_canvas.figure.colorbar(self.ax)
                    self.map_canvas.draw()
    
    
    if __name__ == "__main__":
    
            app = QtWidgets.QApplication.instance()
            if app is None:
                    app = QtWidgets.QApplication(sys.argv)
    
            window = MainWindow()
            window.show()
    
            sys.exit(app.exec_())

Actual outcome

The exception above

Expected outcome

with no exception

Additional information

No response

Operating system

win11

Matplotlib Version

3.5.2

Matplotlib Backend

QtAgg

Python version

Python 3.9.10

Jupyter version

No response

Installation

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions