[Bug]: Crash when adding clabels to subfigures · Issue #23173 · matplotlib/matplotlib · GitHub
Skip to content

[Bug]: Crash when adding clabels to subfigures #23173

Description

@brckts

Bug summary

Adding a clabel to a contour plot of a subfigure results in a traceback.

Code for reproduction

# Taken from the Contour Demo example
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = np.exp(-(X**2) - Y**2)
Z2 = np.exp(-((X - 1) ** 2) - (Y - 1) ** 2)
Z = (Z1 - Z2) * 2

fig = plt.figure()
figs = fig.subfigures(nrows=1, ncols=2)

for f in figs:
    ax = f.subplots()
    CS = ax.contour(X, Y, Z)
    ax.clabel(CS, inline=True, fontsize=10)
    ax.set_title("Simplest default with labels")

plt.show()

Actual outcome

    ax.clabel(CS, inline=True, fontsize=10)
  File "/usr/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 6335, in clabel
    return CS.clabel(levels, **kwargs)
  File "/usr/lib/python3.9/site-packages/matplotlib/contour.py", line 235, in clabel
    self.labels(inline, inline_spacing)
  File "/usr/lib/python3.9/site-packages/matplotlib/contour.py", line 582, in labels
    lw = self._get_nth_label_width(idx)
  File "/usr/lib/python3.9/site-packages/matplotlib/contour.py", line 285, in _get_nth_label_width
    .get_window_extent(mpl.tight_layout.get_renderer(fig)).width)
  File "/usr/lib/python3.9/site-packages/matplotlib/tight_layout.py", line 206, in get_renderer
    if fig._cachedRenderer:
AttributeError: 'SubFigure' object has no attribute '_cachedRenderer'

Expected outcome

The two subfigures appearing side by side, each showing the Contour Demo example

Additional information

No response

Operating system

Gentoo

Matplotlib Version

3.5.2

Matplotlib Backend

QtAgg

Python version

3.9.13

Jupyter version

No response

Installation

Linux package manager

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions