Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brckts opened this issue Jun 1, 2022 · 1 comment · Fixed by #23174
Closed

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

brckts opened this issue Jun 1, 2022 · 1 comment · Fixed by #23174

Comments

@brckts
Copy link

brckts commented Jun 1, 2022

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

@oscargus oscargus added this to the v3.5.3 milestone Jun 1, 2022
@oscargus
Copy link
Member

oscargus commented Jun 1, 2022

Not sure if one should add self._cachedRenderer = None to FigureBase (and remove in Figure) or to SubFigure init-functions, but that should fix it.

I thought it was a recent regression, but it doesn't look like it, so maybe should be labelled 3.6.0 instead?

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

Successfully merging a pull request may close this issue.

2 participants