FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[ENH]: Make it so that linestyle works the same way as color when plotting several bars, lines, scatter points, etc. · Issue #23222 · matplotlib/matplotlib · GitHub

Repository navigation

[ENH]: Make it so that linestyle works the same way as color when plotting several bars, lines, scatter points, etc. #23222

Closed

Description

Problem

Hello!

I am trying to plot a series of bars within a bar plot, and I want each bar to have its own color and linestyle. I intend to achieve this by using a list of colors, and a list of linestyles, and then simply plot the bars and set the color and linestyle to their respective lists. This works for color, but not for linestyle, since the linestyle function does not recognize a list as input.

Below I have two code snippets, and their respective outputs, which highlights the problem.

First, without using linestyle:

Then, using linestyle:

I have checked that all of the linestyles are compatible with the plt.bar function.

Proposed solution

No response

Activity

  1. oscargus commented on Jun 8, 2022

    Member

    Some parameters are dealt with here and a cycler is created which is used to loop over for each rectangle patch:

    linewidth = itertools.cycle(np.atleast_1d(linewidth))
    hatch = itertools.cycle(np.atleast_1d(hatch))
    color = itertools.chain(itertools.cycle(mcolors.to_rgba_array(color)),
    # Fallback if color == "none".
    itertools.repeat('none'))
    if edgecolor is None:
    edgecolor = itertools.repeat(None)
    else:
    edgecolor = itertools.chain(
    itertools.cycle(mcolors.to_rgba_array(edgecolor)),
    # Fallback if edgecolor == "none".
    itertools.repeat('none'))

    The remaining parameters are added to the rectangle patch here:

    r._internal_update(kwargs)

    I see two possible solutions:

    1. Create a cycler for the linestyles/dashpatterns.
    2. Add the remaining parameters to the BarCollection instead. (Not sure this works though.) It is a BarContainer, not a BarCollection.

    I'll take this into consideration in #23056 and see if something can be done to make simplify this procedure.

  2. timhoffm commented on Jun 8, 2022

    Member

    I think it has been a design mistake that bar() can style individual bars separately. Conceptually, all bars from one call belong to one logical data set (which has multiple values). For example, one can only set one label.

    Therefore I suggest not to expand further in this direction. We generally don't want trivial vectorization on the methods as that increases implementation complexity.

    for x, y, color, linestyle in zip([1, 1.25, 1.5, 1.75], [2, 2, 2, 2], colors, linestyles):
        ax.bar(x, y, 0.05, color=color, linestyle=linestyle)
    

    seems simple enough.

  3. timhoffm commented on Jul 6, 2022

    Member

    @AndreasBugten thanks for the suggestion. As laid out in the comment above, I don't want to expand the bar() API to style individual bars separately.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL