Skip to content

[Bug]: Unexpected behavior of the line style specifiers in the histogram method #29857

Closed as duplicate

Description

@mramospe

Bug summary

There is a strange change on the behavior of the line style specifiers of the hist method in matplotlib when moving from version 3.9.0 to 3.10.1. It seems like the linestyle and ls arguments are behaving differently. In addition, it is no longer possible to specify a customized line style for histograms via a tuple ls=(0, (1, 10)). This is not the case for the plot method.

In matplotlib 3.9.0:

Image

In matplotlib 3.10.1:

Image

Using the linestyle argument with (0, (1, 10)) as a value leads to the following error

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

Note that when using ls there is no error, but the result is not what we would expect.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

x = np.random.normal(0, 1, 10_000)

fig, ((ax0, ax1), (ax2, ax3)) = plt.subplots(2, 2, figsize=(8, 8))
ax0.plot(x, x, ls=(0, (1, 10)), lw=2)
ax0.set_title("scatter plot (ls)")
ax1.plot(x, x, linestyle=(0, (1, 10)), lw=2)
ax1.set_title("scatter plot (linestyle)")
# this does not display any error, but the histogram is incorrect
ax2.hist(x, bins=10, range=(-5, +5), ls=(0, (1, 10)), histtype="step", lw=2)
ax2.set_title("hist (ls)")
# this gives an error in 3.10.1
ax3.hist(x, bins=10, range=(-5, +5), linestyle=(0, (1, 10)), histtype="step", lw=2)
ax3.set_title("hist (linestyle)")
plt.show()

Actual outcome

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

Expected outcome

Image

Additional information

No response

Operating system

No response

Matplotlib Version

3.10.1

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Installation

None

Activity

  1. rcomer commented on Apr 2, 2025

    @rcomer
    Member

    Thanks for the report @mramospe. The difference between ls and linestyle was fixed by #29552 and the ValueError was fixed by #29719.

  2. added this to the v3.10.2 milestone on Apr 2, 2025
  3. mramospe commented on Apr 2, 2025

    @mramospe
    Author

    Thanks a lot @rcomer. I apologize since I missed the closed issue. Looking forward to the new release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions