Skip to content

[Bug]: plt.clabel raises exception at very low DPI: ValueError: 'codes' must be a 1D list or array with the same length of 'vertices'. Your vertices have shape (2, 2) but your codes have shape (1,) #26971

@lpsinger

Description

@lpsinger

Bug summary

This worked up to and including Matplotlib 3.7.3 and broke in Matplotlib 3.8.0.

Data files for sample code below:
x.txt
y.txt
z.txt

Code for reproduction

import matplotlib
matplotlib.rcParams['figure.dpi'] = matplotlib.rcParams['savefig.dpi'] = 4

from matplotlib import pyplot as plt
import numpy as np

# See data files attached above: x.txt, y.txt, z.txt
x = np.loadtxt('x.txt')
y = np.loadtxt('y.txt')
z = np.loadtxt('z.txt')

ax = plt.axes()
contourset = ax.contour(x, y, z, levels=[90.0], linewidths=0.5)
ax.clabel(contourset)
plt.savefig('test.pdf')

Actual outcome

Traceback (most recent call last):
  File "/private/tmp/test.py", line 13, in <module>
    ax.clabel(contourset)
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/matplotlib/axes/_axes.py", line 6550, in clabel
    return CS.clabel(levels, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/matplotlib/contour.py", line 222, in clabel
    self.labels(inline, inline_spacing)
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/matplotlib/contour.py", line 622, in labels
    rotation, path = self._split_path_and_get_label_rotation(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/matplotlib/contour.py", line 436, in _split_path_and_get_label_rotation
    return angle, Path(xys, codes)
                  ^^^^^^^^^^^^^^^^
  File "/Users/lpsinger/Library/Python/3.11/lib/python/site-packages/matplotlib/path.py", line 135, in __init__
    raise ValueError("'codes' must be a 1D list or array with the "
ValueError: 'codes' must be a 1D list or array with the same length of 'vertices'. Your vertices have shape (2, 2) but your codes have shape (1,)

Expected outcome

test.pdf

Additional information

I discovered this regression in the CI pipeline from one of my own projects, in a test that sets DPI to an extremely low value for performance purposes. https://git.ligo.org/lscsoft/ligo.skymap/-/jobs/2961694

Operating system

macOS

Matplotlib Version

3.8.0

Matplotlib Backend

MacOSX

Python version

3.11.5

Jupyter version

N/A

Installation

pip

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy