matplotlib / matplotlib/matplotlib

[Bug]: Contour segments changed after clabel

Open
#28,631 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

### Bug summary

`ax.clabel` changes the contour segments itself as it removes the label part of the contours.
image

Python 3.11.8 + Matplotlib 3.7.1 doesn't change the contour itself, where as Python 3.11.9 + Matplotlib 3.9.1 changes it. Is this an intended change?

### Code for reproduction

```Python
import numpy as np
import matplotlib.pyplot as plt

size = 1000
sigma_x = 1.
sigma_y = 1.

x = np.linspace(-3, 3, size)
y = np.linspace(-3, 3, size)

xx, yy = np.meshgrid(x, y)
z = (1/(2*np.pi*sigma_x*sigma_y) * np.exp(-(xx**2/(2*sigma_x**2)
+ yy**2/(2*sigma_y**2))))

extent = [x[0], x[-1], y[0], y[-1]]
fig, ax = plt.subplots()
ax.imshow(z, extent=extent)
cs = ax.contour(z, extent=extent, colors='w')
seg1 = cs.allsegs[3][0]
ax.clabel(cs, cs.levels, fmt='%.5f%%', fontsize=15)
seg2 = cs.allsegs[3][0]
plt.show()

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(6, 3))
ax1.plot(seg1[:, 0], seg1[:, 1], marker='.', ms=1)
ax1.set_title('Before ax.clabel')
ax2.plot(seg2[:, 0], seg2[:, 1], marker='.', ms=1)
ax2.set_title('After ax.clabel')
plt.show()
```

### Actual outcome

Contour segments changed after `ax.clabel`. The labeled part is removed from the contour

### Expected outcome

Keep the contour segment unchanged

### Additional information

Python 3.11.8 + Matplotlib 3.7.1 doesn't change the contour itself, where as Python 3.11.9 + Matplotlib 3.9.1 changes it. Is this an intended change?

### Operating system

MacOS

### Matplotlib Version

3.9.1

### Matplotlib Backend

_No response_

### Python version

3.11.9

### Jupyter version

_No response_

### Installation

None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Python reproduction and the ax.clabel entry point, comparing Matplotlib 3.7.1 and 3.9.1 behavior. Inspect how contour segments are represented before and after labeling. Done means determining whether the changed segments are intentional and, if not, preserving the original contour segment while still displaying the label.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data-visualization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.