Issue 1:
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

Issue 2:
Bug summary

I used to be able to use a legend picker as described in this matplotlib tutorial.

It doesn't work anymore. I bet it was an up

Code for reproduction

import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0, 1)
y1 = 2 * np.sin(2*np.pi*t)
y2 = 4 * np.sin(2*np.pi*2*t)

fig, ax = plt.subplots()
ax.set_title('Click on legend line to toggle line on/off')
line1, = ax.plot(t, y1, lw=2, label='1 Hz')
line2, = ax.plot(t, y2, lw=2, label='2 Hz')
leg = ax.legend(fancybox=True, shadow=True)

lines = [line1, line2]
lined = {} # Will map legend lines to original lines.
for legline, origline in zip(leg.get_lines(), lines):
legline.set_picker(True) # Enable picking on the legend line.
lined[legline] = origline

def on_pick(event):
# On the pick event, find the original line corresponding to the legend
# proxy line, and toggle its visibility.
legline = event.artist
origline = lined[legline]
visible = not origline.get_visible()
origline.set_visible(visible)
# Change the alpha on the line in the legend so we can see what lines
# have been toggled.
legline.set_alpha(1.0 if visible else 0.2)
fig.canvas.draw()

fig.canvas.mpl_connect('pick_event', on_pick)
plt.show()

Actual outcome

It graphs fine, but I can't toggle visibility by clicking the legend.

I've tried executing the code from my Mac Terminal and from an IDE (Spyder) with Python 3.8.12 and matplotlib version 3.5.1.

Expected outcome

I should be able to click and toggle the visibility.

Additional information

Legend picking with this script still works from my other Spyder app, which has python 3.9.2 and matplotlib 3.3.4.

Operating system

macOS Monterey

Matplotlib Version

3.5.1

Matplotlib Backend

MacOSX

Python version

3.8.12

Jupyter version

No response

Installation

conda