Description
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
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
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