Skip to content

[Bug]: Can't add contour labels with "manual=True" #22763

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
azahel789 opened this issue Apr 1, 2022 · 4 comments
Closed

[Bug]: Can't add contour labels with "manual=True" #22763

azahel789 opened this issue Apr 1, 2022 · 4 comments

Comments

@azahel789
Copy link

Bug summary

The contour label (clabel) manual mode fails place any labels. When I click the plot I get an error:
'QuadContourSet' object has no attribute 'ax'
and no labels are added.
I put the Contour Label Demo as an example with clabel option manual=True, but this has happened with different scripts.

Code for reproduction

import numpy as np
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt


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
# This custom formatter removes trailing zeros, e.g. "1.0" becomes "1", and
# then adds a percent sign.
def fmt(x):
    s = f"{x:.1f}"
    if s.endswith("0"):
        s = f"{x:.0f}"
    return rf"{s} \%" if plt.rcParams["text.usetex"] else f"{s} %"


# Basic contour plot
fig, ax = plt.subplots()
CS = ax.contour(X, Y, Z)

ax.clabel(CS, CS.levels, inline=True, fmt=fmt, fontsize=10, manual=True)

plt.show()

Actual outcome

contour
I get the following error:
Traceback (most recent call last):
File "/Users/azahel/anaconda3/lib/python3.9/site-packages/matplotlib/cbook/init.py", line 287, in process
func(*args, **kwargs)
File "/Users/azahel/anaconda3/lib/python3.9/site-packages/matplotlib/contour.py", line 70, in _contour_labeler_event_handler
if event.inaxes == cs.ax:
AttributeError: 'QuadContourSet' object has no attribute 'ax'

Expected outcome

image

Additional information

I recently upgraded matplotlib to 3.5.1, I'm not sure if this was a problem with previous versions.
Checking the file contour.py it is clear that ContourSet, and by extension QuadContourSet, do not have the attribute "ax" but rather "axes".
Changing line 70 from contour.py
" if event.inaxes == cs.ax:"
to
" if event.inaxes == cs.axes:"
resolves the issue.
Seeing in the documentation that Contour set is described to have the attribute "ax", perhaps the real fix would be to change ContourSet class.

Operating system

OS/X

Matplotlib Version

3.5.1

Matplotlib Backend

MacOSX

Python version

3.9.12

Jupyter version

No response

Installation

conda

@jklymak
Copy link
Member

jklymak commented Apr 1, 2022

I think this is a duplicate of #22760 - events were broken on MacOS, but are working on the dev build (upcoming 3.6). Please use Qt5Agg, the dev version of Matplotlib or roll back to 3.4.x....

@azahel789
Copy link
Author

I have tried it now with Qt5Agg and the issue persists, unless I do the fix I proposed. I'm also aware that the event handlers with MacOS are broken, but I think this is a separate issue.

@tacaswell tacaswell reopened this Apr 1, 2022
@jklymak
Copy link
Member

jklymak commented Apr 1, 2022

Sorry, yes that has been fixed on main and will be in 3.5.2

#22534

@jklymak jklymak closed this as completed Apr 1, 2022
@tacaswell
Copy link
Member

Ah, sorry should should tracked down the changes before re-opening 🐑

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

No branches or pull requests

3 participants