Skip to content

[ENH]: scalermappable bar #26312

Open
Open

Description

@story645

Problem

Coloring the bars in the bar chart based on some value isn't uncommon (#23265, #26310) and colors=cmap(norm(values)) is serviceable but it'd be nice if folks could directly set the cmap, norm, and data separately-especially for legend and colorbar creation.

Proposed solution

While it has it's problems, scatter has an API for how to manage colormap/norm or colors for discrete markers, so I think it would stay consistent if the scatter API for colors could be ported to bars. Could then probably adapt scatter.legend_elements implementation to auto generate legends from the colors.

ETA: If we did this, it'd probably also be a good reason to implement #7383

Activity

  1. anntzer commented on Jul 15, 2023

    @anntzer
    Contributor

    While it is not strictly necessary, I think this would be conceptually related to moving bar() to return a single PathCollection (#22976), which is a fair bit of work but which I now think could be done in a way similar to the ContourSet transition recently merged (#25247).

  2. timhoffm commented on Jul 16, 2023

    @timhoffm
    Member

    Gradient bars

    Note that #23265 is asking for "draw 3d bars gradient", which is the second example in https://de.mathworks.com/help/matlab/creating_plots/color-3-d-bars-by-height-1.html.

    Even with a scalarmappable bar, we'll not be able to support this. One could achive this with a value-scaled variation of https://matplotlib.org/stable/gallery/lines_bars_and_markers/gradient_bar.html#sphx-glr-gallery-lines-bars-and-markers-gradient-bar-py. But I think we're far from internal code structures or an API needed to include this as a builtin feature.

    For now, I'd say, this are out of scope.


    Color-coded bars

    This is fundamentally reasonable, even though #26310 is not making a good case for it.

    We have basically two cases:

    1. discrete colors that show up in the legend.

    Examples:

    These are already handled via https://matplotlib.org/stable/gallery/lines_bars_and_markers/bar_colors.html

    2. Continuous colors.

    I've found two classes of examples:

    • Purely artistic ones
      image
      These do not need a legend and thus,

      colors = plt.cm.viridis(np.linspace(0, 1, len(labels)))
      plt.bar(labels, values, colors=colors)
      

      is enough. (Apart from the question whether one should do this at all).

    • Continous color coding:
      e.g.
      image|200

    This needs a colorbar as reference and is the only case possibly warranting an extension of our API. Be aware though that bars have color, edgecolor and ecolor (errorbars). And that we need a conscious decision how to handle them (likely only provide colormapping for the face color).

    I think this would be conceptually related to moving bar() to return a single PathCollection

    rather replace BarContainer.patches (currently list of Rectangle) by a collection. (t.b.d. wether a general PathCollection is suitable or whether a dedicated RectangleCollection is justified.

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