[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
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.
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)
Activity
oscargus commented on Jun 8, 2022
Some parameters are dealt with here and a cycler is created which is used to loop over for each rectangle patch:
matplotlib/lib/matplotlib/axes/_axes.py
Lines 2381 to 2392 in 8e2987b
The remaining parameters are added to the rectangle patch here:
matplotlib/lib/matplotlib/axes/_axes.py
Line 2430 in 8e2987b
I see two possible solutions:
I'll take this into consideration in #23056 and see if something can be done to make simplify this procedure.
timhoffm commented on Jun 8, 2022
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.
timhoffm commented on Jul 6, 2022
@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.