Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

matplotlib basemap colorbar exception : Given element not contained in the stack

180 views
Skip to first unread message

iMath

unread,
May 29, 2022, 8:56:20 AM5/29/22
to
I am new to matplotlib, I got exception

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




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_())

iMath

unread,
May 29, 2022, 8:57:30 AM5/29/22
to

MRAB

unread,
May 29, 2022, 3:29:28 PM5/29/22
to
The problem might be that you're passing "ax=self.ax" when you create
the basemap, and you have:

self.ax = self.map_canvas.figure.subplots()

According to the docs, ".subplots" doesn't return the axes, it returns a
_tuple_ of the figure and the axes.

iMath

unread,
May 30, 2022, 2:41:46 AM5/30/22
to
Thanks for your reply ! But as I printed the value of self.ax , it is actually an instance of AxesSubplot.
0 new messages