Skip to content

[Bug]: Different behaviour between showing and savefig PDF #31585

@Nebulean

Description

@Nebulean

Bug summary

Hi there,
I came across an error that was tricky to debug, as it had some unexpected behaviour. Basically, I used ls="" in a scatter plot, which rise an issue if i understand it correctly. But when plt.show(), the image is displayed perfectly, while plt.savefig for PDF specifically crashes with the unreadable error below (ValueError: zero-size array to reduction operation maximum which has no identity).

You can find the minimum example below. Please uncomment plt.show() for no error.

Code for reproduction

plt.figure()
plt.scatter([0, 1], [0, 1], ls="")
#plt.show()
plt.savefig("test.pdf")

Actual outcome

Traceback (most recent call last):
  File "<user_script>.py", line <line_number>, in <module>
    <user_function>()
  File "<user_script>.py", line <line_number>, in <user_function>
    plt.savefig("<output_file>.pdf")
  File "<python_env>/lib/python3.11/site-packages/matplotlib/pyplot.py", line 1228, in savefig
    res = fig.savefig(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<python_env>/lib/python3.11/site-packages/matplotlib/figure.py", line 3395, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "<python_env>/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 2204, in print_figure
    result = print_method(
             ^^^^^^^^^^^^^
  File "<python_env>/lib/python3.11/site-packages/matplotlib/backend_bases.py", line 2054, in <lambda>
    print_method = functools.wraps(meth)(lambda *args, **kwargs: meth(
                                                                 ^^^^^
  File "<python_env>/lib/python3.11/site-packages/matplotlib/backends/backend_pdf.py", line 2799, in print_pdf
    self.figure.draw(renderer)
  File "<python_env>/lib/python3.11/site-packages/matplotlib/artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<python_env>/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<python_env>/lib/python3.11/site-packages/matplotlib/figure.py", line 3162, in draw
    mimage._draw_list_compositing_images(
  File "<python_env>/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "<python_env>/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<python_env>/lib/python3.11/site-packages/matplotlib/axes/_base.py", line 3137, in draw
    mimage._draw_list_compositing_images(
  File "<python_env>/lib/python3.11/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "<python_env>/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<python_env>/lib/python3.11/site-packages/matplotlib/collections.py", line 1005, in draw
    super().draw(renderer)
  File "<python_env>/lib/python3.11/site-packages/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<python_env>/lib/python3.11/site-packages/matplotlib/collections.py", line 426, in draw
    renderer.draw_path_collection(
  File "<python_env>/lib/python3.11/site-packages/matplotlib/backends/backend_pdf.py", line 2074, in draw_path_collection
    padding = np.max(linewidths)
              ^^^^^^^^^^^^^^^^^^^^^^^
  File "<python_env>/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 2810, in max
    return _wrapreduction(a, np.maximum, 'max', axis, None, out,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<python_env>/lib/python3.11/site-packages/numpy/core/fromnumeric.py", line 88, in _wrapreduction
    return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: zero-size array to reduction operation maximum which has no identity

Expected outcome

The error should be on the use of lw="" in the scatter plot, but it should be coherent with the plt.show (which is not the case at the moment)

Additional information

I don't know where the bug comes from, but I assume that "ls" is defined as a parameter of scatter, but is not uniformely treated in between the interactive window and the pdf backend.

Operating system

MacOS 14.8.2, M1 pro processor.

Matplotlib Version

3.9.2

Matplotlib Backend

macosx

Python version

3.11.10

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions