Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: stacked histogram does not properly handle edgecolor and hatches #26718

Open
marcelernst opened this issue Sep 7, 2023 · 5 comments · May be fixed by #28073
Open

[Bug]: stacked histogram does not properly handle edgecolor and hatches #26718

marcelernst opened this issue Sep 7, 2023 · 5 comments · May be fixed by #28073

Comments

@marcelernst
Copy link

marcelernst commented Sep 7, 2023

Bug summary

When using stacked histogram in matplotlib 3.7.2 the functionality of hatch and edgecolor is broken. It simply gives an error if trying to define either a separate edgecolor or hatch value for the stacked parts of the histogram.

Code for reproduction

import matplotlib.pyplot as plt
import numpy as np

plt.hist([np.random.uniform(size=100), np.random.uniform(size=50)],
         stacked=True,
         color=["C0", "C1"],
         edgecolor='black',
         hatch=["/", "."],
         linewidth=1,
         fill=True,
         density=False, 
         )

Actual outcome

Traceback (most recent call last):
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3460, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
plt.hist([np.random.uniform(size=100), np.random.uniform(size=50)],
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/pyplot.py", line 2645, in hist
return gca().hist(
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/init.py", line 1446, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/axes/_axes.py", line 6944, in hist
p._internal_update(kwargs)
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/artist.py", line 1223, in _internal_update
return self._update_props(
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/artist.py", line 1199, in _update_props
ret.append(func(v))
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/patches.py", line 525, in set_hatch
mhatch._validate_hatch_pattern(hatch)
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/hatch.py", line 188, in _validate_hatch_pattern
invalids = ''.join(sorted(invalids))
TypeError: sequence item 0: expected str instance, NoneType found

While using different edgecolors for the stacked part with

plt.hist([np.random.uniform(size=100), np.random.uniform(size=50)],
         stacked=True,
         color=["C0", "C1"],
         edgecolor=["C2", "C3"],
         hatch="/",
         linewidth=1,
         fill=True,
         density=False, 
         )

delivers the error

Traceback (most recent call last):
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3460, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
plt.hist([np.random.uniform(size=100), np.random.uniform(size=50)],
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/pyplot.py", line 2645, in hist
return gca().hist(
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/init.py", line 1446, in inner
return func(ax, *map(sanitize_sequence, args), **kwargs)
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/axes/_axes.py", line 6944, in hist
p._internal_update(kwargs)
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/artist.py", line 1223, in _internal_update
return self._update_props(
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/artist.py", line 1199, in _update_props
ret.append(func(v))
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/patches.py", line 341, in set_edgecolor
self._set_edgecolor(color)
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/patches.py", line 327, in _set_edgecolor
self._edgecolor = colors.to_rgba(color, self._alpha)
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/colors.py", line 299, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "/opt/anaconda3/envs/code/lib/python3.10/site-packages/matplotlib/colors.py", line 383, in _to_rgba_no_colorcycle
raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4

It seems to interpret the list of edgecolor ["C2", "C3"] as one color, although it is able to manage similar input for color.

Expected outcome

It should look similar to the following example, except that the top orange part should have dots and not slanted lines:
test_bugreport
The code used to generate this plot:

import matplotlib.pyplot as plt
import numpy as np
plt.hist([np.random.uniform(size=100), np.random.uniform(size=50)],
         stacked=True,
         color=["C0", "C1"],
         edgecolor='black',
         hatch="/",
         linewidth=1,
         fill=True,
         density=False, 
         )

In case of the second example it should habe two different edgecolors for the first and second part of the stacked histogram.

Additional information

No response

Operating system

MacOSX

Matplotlib Version

3.7.2

Matplotlib Backend

MacOSX

Python version

3.10.0

Jupyter version

No response

Installation

conda

@stevezhang1999
Copy link
Contributor

Didn't have the chance to test on Python 3.10.0 and mpl 3.7.2 yet. But running first block of the code above with mpl 3.9.0 dev and Python 3.11.5 (conda) on Mac will give:

Traceback (most recent call last):
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/backend_bases.py", line 1215, in _on_timer
    ret = func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/backends/backend_macosx.py", line 70, in callback_func
    callback()
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/backends/backend_macosx.py", line 93, in _draw_idle
    self.draw()
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/backends/backend_macosx.py", line 52, in draw
    super().draw()
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/backends/backend_agg.py", line 387, in draw
    self.figure.draw(self.renderer)
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/artist.py", line 95, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/figure.py", line 3101, in draw
    mimage._draw_list_compositing_images(
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/axes/_base.py", line 3095, in draw
    mimage._draw_list_compositing_images(
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/artist.py", line 72, in draw_wrapper
    return draw(artist, renderer)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/patches.py", line 588, in draw
    self._draw_paths_with_artist_properties(
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/patches.py", line 573, in _draw_paths_with_artist_properties
    renderer.draw_path(gc, *draw_path_args)
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/backends/backend_agg.py", line 131, in draw_path
    self._renderer.draw_path(gc, path, transform, rgbFace)
  File "/Users/xxxxxx/Documents/matplotlib-project/matplotlib/lib/matplotlib/backend_bases.py", line 1018, in get_hatch_path
    return Path.hatch(hatch, density)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'list'

@fernandaitoda
Copy link

Hi, I'm working in this issue if @stevezhang1999 is not . Right now 'color' is a parameter in hist function, but edgecolor/hatch isn't. The function that deals with this 2 other parameters is in artist.py, and accepts types 'color' or 'none'. I was thinking about adding these parameters in hist function, but that means we would have to change documentation. If someone have a better approach pls tell me

@stevezhang1999
Copy link
Contributor

Hi, I'm working in this issue if @stevezhang1999 is not . Right now 'color' is a parameter in hist function, but edgecolor/hatch isn't. The function that deals with this 2 other parameters is in artist.py, and accepts types 'color' or 'none'. I was thinking about adding these parameters in hist function, but that means we would have to change documentation. If someone have a better approach pls tell me

You are welcomed to work on it if that interests you!

@fernandaitoda
Copy link

hey guys, in developer mode plt.show() doesn't work. Do you know how can I see the plots so I know if my code is working correctly?

@jklymak
Copy link
Member

jklymak commented Oct 15, 2023

There is no reason for plt.show to not work in developer mode. Check your installation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants