Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5bab18d

Browse filesBrowse files
committed
Add colors and kwargs, typing stubs, pyplot wrapper
1 parent c873f4d commit 5bab18d
Copy full SHA for 5bab18d

File tree

5 files changed

+91
-7
lines changed
Filter options

5 files changed

+91
-7
lines changed

‎galleries/examples/lines_bars_and_markers/grouped_bar_chart.py

Copy file name to clipboardExpand all lines: galleries/examples/lines_bars_and_markers/grouped_bar_chart.py
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@
145145
axs[1, 1].grouped_bar(x, data, group_spacing=0.5, bar_spacing=0.1)
146146

147147

148+
# %%
149+
# Styling
150+
# -------
151+
# The bars can be styled through additional keyword arguments. Currently,
152+
# the only per-dataset setting is ``colors``. Additionally, all
153+
# `.Rectangle parameters` are passed through and applied to all datasets.
154+
155+
x = ['A', 'B', 'C']
156+
data = {
157+
'data1': [1, 2, 3],
158+
'data2': [1.2, 2.2, 3.2],
159+
'data3': [1.4, 2.4, 3.4],
160+
'data4': [1.6, 2.6, 3.6],
161+
}
162+
163+
fig, ax = plt.subplots()
164+
ax.grouped_bar(x, data, colors=["r", "g", "b", "m"], edgecolor="black")
165+
166+
148167
# %%
149168
# Horizontal grouped bars
150169
# -----------------------

‎lib/matplotlib/axes/_axes.py

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+35-7Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3014,10 +3014,17 @@ def broken_barh(self, xranges, yrange, **kwargs):
30143014
return col
30153015

30163016
def grouped_bar(self, x, heights, *, group_spacing=1.5, bar_spacing=0,
3017-
dataset_labels=None, orientation="vertical"):
3017+
dataset_labels=None, orientation="vertical", colors=None,
3018+
**kwargs):
30183019
"""
3020+
Make a grouped bar plot.
3021+
3022+
.. note::
3023+
This function is new in v3.10, and the API is still provisional.
3024+
We may still fine-tune some aspects based on user-feedback.
3025+
30193026
Parameters
3020-
-----------
3027+
----------
30213028
x : array-like or list of str
30223029
The center positions of the bar groups. If these are numeric values,
30233030
they have to be equidistant. As with `~.Axes.bar`, you can provide
@@ -3081,9 +3088,20 @@ def grouped_bar(self, x, heights, *, group_spacing=1.5, bar_spacing=0,
30813088
The labels of the datasets.
30823089
30833090
orientation : {"vertical", "horizontal"}, default: vertical
3084-
"""
3085-
_api.check_in_list(["vertical", "horizontal"], orientation=orientation)
30863091
3092+
colors : list of :mpltype:`color`, optional
3093+
A sequence of colors to be cycled through and used to color bars
3094+
of the different datasets. The sequence need not be exactly the
3095+
same length as the number of provided y, in which case the colors
3096+
will repeat from the beginning.
3097+
3098+
If not specified, the colors from the Axes property cycle will be used.
3099+
3100+
**kwargs : `.Rectangle` properties
3101+
3102+
%(Rectangle:kwdoc)s
3103+
3104+
"""
30873105
if hasattr(heights, 'keys'):
30883106
if dataset_labels is not None:
30893107
raise ValueError(
@@ -3118,6 +3136,15 @@ def grouped_bar(self, x, heights, *, group_spacing=1.5, bar_spacing=0,
31183136
f"has {len(dataset)} groups"
31193137
)
31203138

3139+
_api.check_in_list(["vertical", "horizontal"], orientation=orientation)
3140+
3141+
if colors is None:
3142+
colors = itertools.cycle([None])
3143+
else:
3144+
# Note: This is equivalent to the behavior in stackplot
3145+
# TODO: do we want to be more restrictive and check lengths?
3146+
colors = itertools.cycle(colors)
3147+
31213148
bar_width = (group_distance /
31223149
(num_datasets + (num_datasets - 1) * bar_spacing + group_spacing))
31233150
bar_spacing_abs = bar_spacing * bar_width
@@ -3130,15 +3157,16 @@ def grouped_bar(self, x, heights, *, group_spacing=1.5, bar_spacing=0,
31303157

31313158
# place the bars, but only use numerical positions, categorical tick labels
31323159
# are handled separately below
3133-
for i, (hs, dataset_label) in enumerate(zip(heights, dataset_labels)):
3160+
for i, (hs, dataset_label, color) in enumerate(
3161+
zip(heights, dataset_labels, colors)):
31343162
lefts = (group_centers - 0.5 * group_distance + margin_abs
31353163
+ i * (bar_width + bar_spacing_abs))
31363164
if orientation == "vertical":
31373165
self.bar(lefts, hs, width=bar_width, align="edge",
3138-
label=dataset_label)
3166+
label=dataset_label, color=color, **kwargs)
31393167
else:
31403168
self.barh(lefts, hs, height=bar_width, align="edge",
3141-
label=dataset_label)
3169+
label=dataset_label, color=color, **kwargs)
31423170

31433171
if tick_labels is not None:
31443172
if orientation == "vertical":

‎lib/matplotlib/axes/_axes.pyi

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.pyi
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,17 @@ class Axes(_AxesBase):
284284
data=...,
285285
**kwargs
286286
) -> PolyCollection: ...
287+
def grouped_bar(
288+
self,
289+
x : ArrayLike,
290+
heights : ArrayLike,
291+
group_spacing : float | None = ...,
292+
bar_spacing : float | None = ...,
293+
dataset_labels : Sequence[str] | None = ...,
294+
orientation: Literal["vertical", "horizontal"] = ...,
295+
colors: Iterable[ColorType] | None = ...,
296+
**kwargs
297+
) -> None: ...
287298
def stem(
288299
self,
289300
*args: ArrayLike | str,

‎lib/matplotlib/pyplot.py

Copy file name to clipboardExpand all lines: lib/matplotlib/pyplot.py
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3376,6 +3376,31 @@ def grid(
33763376
gca().grid(visible=visible, which=which, axis=axis, **kwargs)
33773377

33783378

3379+
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
3380+
@_copy_docstring_and_deprecators(Axes.grouped_bar)
3381+
def grouped_bar(
3382+
x: ArrayLike,
3383+
heights: ArrayLike,
3384+
*,
3385+
group_spacing: float | None = 1.5,
3386+
bar_spacing: float | None = 0,
3387+
dataset_labels: Sequence[str] | None = None,
3388+
orientation: Literal["vertical", "horizontal"] = "vertical",
3389+
colors: Iterable[ColorType] | None = None,
3390+
**kwargs,
3391+
) -> None:
3392+
gca().grouped_bar(
3393+
x,
3394+
heights,
3395+
group_spacing=group_spacing,
3396+
bar_spacing=bar_spacing,
3397+
dataset_labels=dataset_labels,
3398+
orientation=orientation,
3399+
colors=colors,
3400+
**kwargs,
3401+
)
3402+
3403+
33793404
# Autogenerated by boilerplate.py. Do not edit as changes will be lost.
33803405
@_copy_docstring_and_deprecators(Axes.hexbin)
33813406
def hexbin(

‎tools/boilerplate.py

Copy file name to clipboardExpand all lines: tools/boilerplate.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ def boilerplate_gen():
238238
'fill_between',
239239
'fill_betweenx',
240240
'grid',
241+
'grouped_bar',
241242
'hexbin',
242243
'hist',
243244
'stairs',

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.