Skip to content

[Bug]: bar chart with variable fill not working #29072

Closed

Description

@igurin-invn

Bug summary

An array-like fill argument to plt.bar has no effect.

Code for reproduction

plt.bar([1, 2, 3], [1, 2, 3], fill=(False, True, False), color=('C1', 'C2', 'C3'))

Actual outcome

image

Expected outcome

image

Additional information

No response

Operating system

Windows

Matplotlib Version

3.8.4

Matplotlib Backend

'module://matplotlib_inline.backend_inline'

Python version

3.12.4

Jupyter version

7.2.2

Installation

conda

Activity

  1. jklymak commented on Nov 5, 2024

    @jklymak
    Member

    Did this work in the past? Fill is passed down ti Rectangle and I don't think it was ever done on a per-Rectangle basis, but I could be wrong.

  2. igurin-invn commented on Nov 5, 2024

    @igurin-invn
    ContributorAuthor

    I haven't tried this before.

  3. QuLogic commented on Nov 5, 2024

    @QuLogic
    Member

    fill is not documented as being vectorized like that.

  4. timhoffm commented on Nov 5, 2024

    @timhoffm
    Member

    You can achieve the same effect by specifying facecolor and edgecolor, which both accept sequences. Use "none" as facecolor for the unfolded bars.

  5. Satyanarayana3012 commented on Nov 5, 2024

    @Satyanarayana3012
    import matplotlib.pyplot as plt
    
    x = [1, 2, 3]
    y = [1, 2, 3]
    
    fill_colors = ['none', 'C2', 'none']
    outline_colors = ['orange', 'green', 'pink']
    
    for i in range(len(x)):
        plt.bar(
            x[i], 
            y[i], 
            color=fill_colors[i],      
            edgecolor=outline_colors[i] 
        )
    
    plt.show()

    github
    Try this code

  6. rcomer commented on Nov 5, 2024

    @rcomer
    Member

    @Satyanarayana3012 you can avoid the loop with

    plt.bar(x, y, color=fill_colors, edgecolor=outline_colors)
  7. Satyanarayana3012 commented on Nov 5, 2024

    @Satyanarayana3012

    @Satyanarayana3012 you can avoid the loop with

    plt.bar(x, y, color=fill_colors, edgecolor=outline_colors)

    Okay

  8. timhoffm commented on Nov 5, 2024

    @timhoffm
    Member

    I prefer not to extend the fill parameter to support sequences. bar() currently uses a list or Rectangles as artists, and so individual fills would be possible. However, we may migrate to a Collection at some point. And collections do not support the fill property. The canonical apporach, which is also used in Collections is to use a sequence of facecolors and set unfilled elements to "none".

    I therefore close this.

  9. igurin-invn commented on Nov 5, 2024

    @igurin-invn
    ContributorAuthor

    The documentation does not identify any of the kwargs as accepting sequences, so I don't understand how I was supposed to figure this out.

    I also don't understand the solution. All of the following produced errors.

     plt.bar([1, 2, 3], [1, 2, 3], color=('C1', 'C2', 'C3'), facecolor=(None, 'C2', None))
     plt.bar([1, 2, 3], [1, 2, 3], color=('C1', 'C2', 'C3'), facecolor=('none', 'C2', 'none'))
     plt.bar([1, 2, 3], [1, 2, 3], edgecolor=('C1', 'C2', 'C3'), facecolor=('none', 'C2', 'none'))
     plt.bar([1, 2, 3], [1, 2, 3], edgecolor=('C1', 'C2', 'C3'), facecolor=['none', 'C2', 'none'])
  10. jklymak commented on Nov 5, 2024

    @jklymak
    Member

    Yeah, I'll take the liberty of reopening. facecolor definitely doesn't work, though color instead of facecolor works fine. Not sure if this is a behavior issue or just documentation. It's a bit goofy that edgecolor works, but facecolor does not, in my opinion.

  11. reopened this on Nov 5, 2024
  12. igurin-invn commented on Nov 5, 2024

    @igurin-invn
    ContributorAuthor

    Thanks!

    For the record, the single-line solution is:

    plt.bar([1, 2, 3], [1, 2, 3], color=['none', 'C2', 'none'], edgecolor=['C1', 'C2', 'C3'])

    Note also that 'none' is not a documented color. (Neither is None, which does not in fact work, but is reasonable to try.)

  13. igurin-invn commented on Nov 5, 2024

    @igurin-invn
    ContributorAuthor

    This is a bit down in the weeds, but a workaround using only documented colors is:

    plt.bar([1, 2, 3], [1, 2, 3], color=('w', 'C2', 'w'), edgecolor=('C1', 'C2', 'C3'))

    This is not identical (considering the possibility of a non-white background), and it does still use the undocumented vectorization of edgecolor.

  14. rcomer commented on Nov 5, 2024

    @rcomer
    Member

    The documentation does state that color and edgecolor may be lists.

    image

    I agree we should probably have "none" somewhere on that "Specifying colors" page.

  15. igurin-invn commented on Nov 5, 2024

    @igurin-invn
    ContributorAuthor

    OK. It gets weirder, actually. I just noticed that both color and edgecolor are listed under "Other Parameters" and again under kwargs, with different descriptions. The descriptions under kwargs don't mention vectorization. It sounds like it might be really hard to straighten out this documentation issue, though.

    (I know I did read both sets of descriptions earlier, but I just felt vaguely confused instead of noticing the repetition.)

  16. timhoffm commented on Nov 5, 2024

    @timhoffm
    Member

    "none" is now document as a color in #29081.

    facecolor should be made to accept sequences. I've opened #29090 for that.

    color and edgecolor have special handling for accepting the sequences and are therefore listed explicitly. Generally, all kwargs are otherwise passed through to each Rectange - the list of rectangle properties is auto-generated and therefore also contains color and edgecolor in their original form. The auto-generation does not know about the special handling.
    I think the documentation is okish as is, and it's not worth to jump extra hoops to remove the duplication. The specially handled parameters are documented explicitly and more prominently. Their functionality is a superset of the original functionality, so the auto-generated table is not wrong, only incomplete.

    I believe that's all to be done here.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions