Skip to content

[Issue]: Manually placing contour labels using clabel not working #22532

@carmaza

Description

@carmaza

Bug summary

Manually placing contour labels using clabel not working.

Code for reproduction

import matplotlib                                                                                       
import numpy as np                                                                                      
import sys                                                                                              
                                                                                                        
from matplotlib import pyplot as plt                                                                    
                                                                                                        
print("Python version:", sys.version)                                                                   
print("MPL version:", matplotlib.__version__)                                                           
print("NumPy version:", np.__version__)                                                                 
print("MPL backend:", matplotlib.get_backend())                                                         
                                                                                                        
t = np.linspace(0., 1., 100)                                                                            
X, Y = np.meshgrid(t, t)                                                                                
                                                                                                        
contours = plt.contour(X, Y, X*X + Y*Y)                                                                 
plt.clabel(contours, manual=True)                                                             
                                                                                                        
plt.savefig("test.pdf", bbox_inches='tight')

Actual outcome

Running python3 test.py in file location gives the following output:

Python version: 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0]
MPL version: 3.5.1
NumPy version: 1.22.2
MPL backend: GTK3Agg
Select label locations manually using first mouse button.
End manual selection with second mouse button.

Then the figure shows, expecting me to click on it to place labels manually. Upon clicking anywhere on the image, I get:

Traceback (most recent call last):
  File "/home/cristobal/.local/lib/python3.8/site-packages/matplotlib/backends/backend_gtk3.py", line 159, in button_press_event
    FigureCanvasBase.button_press_event(
  File "/home/cristobal/.local/lib/python3.8/site-packages/matplotlib/backend_bases.py", line 1877, in button_press_event
    self.callbacks.process(s, mouseevent)
  File "/home/cristobal/.local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 292, in process
    self.exception_handler(exc)
  File "/home/cristobal/.local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 96, in _exception_printer
    raise exc
  File "/home/cristobal/.local/lib/python3.8/site-packages/matplotlib/cbook/__init__.py", line 287, in process
    func(*args, **kwargs)
  File "/home/cristobal/.local/lib/python3.8/site-packages/matplotlib/contour.py", line 70, in _contour_labeler_event_handler
    if event.inaxes == cs.ax:
AttributeError: 'QuadContourSet' object has no attribute 'ax'

and no label is placed in the contours.

I can close the figure normally using third click button, and the figure is saved normally but without any labels on the contours.

Expected outcome

Clicking on canvas should place labels.

Additional information

I am able to place labels as expected using the following versions:

('Python version:', '2.7.18 (default, Mar  8 2021, 13:02:45) \n[GCC 9.3.0]')
('MPL version:', '2.2.5')
('NumPy version:', '1.16.6')
('MPL backend:', 'TkAgg')

(this is the output after running python test.py).

If this is not a bug, I propose to modify the documentation of clabel (https://matplotlib.org/stable/api/contour_api.html#matplotlib.contour.ContourLabeler.clabel) accordingly.

Operating system

Ubuntu 20.04

Matplotlib Version

3.5.1

Matplotlib Backend

GTK3Agg

Python version

3.8.10

Jupyter version

No response

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions