Skip to content

[ENH]: scalermappable bar #26312

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

Open
story645 opened this issue Jul 14, 2023 · 2 comments
Open

[ENH]: scalermappable bar #26312

story645 opened this issue Jul 14, 2023 · 2 comments

Comments

@story645
Copy link
Member

story645 commented Jul 14, 2023

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

@anntzer
Copy link
Contributor

anntzer commented Jul 15, 2023

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).

@timhoffm
Copy link
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
Projects
None yet
Development

No branches or pull requests

3 participants