You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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).
is enough. (Apart from the question whether one should do this at all).
Continous color coding:
e.g.
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.
Activity
anntzer commentedon 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 commentedon Jul 16, 2023
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

These do not need a legend and thus,
is enough. (Apart from the question whether one should do this at all).
Continous color coding:

e.g.
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).
rather replace
BarContainer.patches(currently list of Rectangle) by a collection. (t.b.d. wether a generalPathCollectionis suitable or whether a dedicatedRectangleCollectionis justified.