Skip to content

[Bug]: contour label overlap algorithm not quite right... #22367

Open
@jklymak

Description

@jklymak

Bug summary

Contour labelling is hard, but our algorithm seems to create lots of overlaps when there is substantial room for non-overlaps.

Code for reproduction

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

fig, ax = plt.subplots(1, 1, figsize=(2, 4))

x = np.linspace(1, 4, 20)
y = np.linspace(1, 4, 20)
X, Y = np.meshgrid(x, y)
cnt = ax.contour(X, Y, X, levels=20)
ax.clabel(cnt, levels=cnt.levels)
plt.show()

Actual outcome

testCnt

Expected outcome

The labels would be staggered vertically.

Additional information

I'm not aware if this ever worked optimally. There was some optimization of the algorithm in #19018, but checking out v3.3.4 doesn't change the result.

Operating system

No response

Matplotlib Version

master...

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions