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 17653a6

Browse filesBrowse files
authored
Merge pull request #28560 from timhoffm/grouped_bar
ENH: Add grouped_bar() method
2 parents 06314bf + e0afe74 commit 17653a6
Copy full SHA for 17653a6

File tree

Expand file treeCollapse file tree

11 files changed

+517
-12
lines changed
Filter options
Expand file treeCollapse file tree

11 files changed

+517
-12
lines changed

‎doc/_embedded_plots/grouped_bar.py

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import matplotlib.pyplot as plt
2+
3+
categories = ['A', 'B']
4+
data0 = [1.0, 3.0]
5+
data1 = [1.4, 3.4]
6+
data2 = [1.8, 3.8]
7+
8+
fig, ax = plt.subplots(figsize=(4, 2.2))
9+
ax.grouped_bar(
10+
[data0, data1, data2],
11+
tick_labels=categories,
12+
labels=['dataset 0', 'dataset 1', 'dataset 2'],
13+
colors=['#1f77b4', '#58a1cf', '#abd0e6'],
14+
)
15+
ax.legend()

‎doc/api/axes_api.rst

Copy file name to clipboardExpand all lines: doc/api/axes_api.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Basic
6767
Axes.bar
6868
Axes.barh
6969
Axes.bar_label
70+
Axes.grouped_bar
7071

7172
Axes.stem
7273
Axes.eventplot

‎doc/api/pyplot_summary.rst

Copy file name to clipboardExpand all lines: doc/api/pyplot_summary.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Basic
6060
bar
6161
barh
6262
bar_label
63+
grouped_bar
6364
stem
6465
eventplot
6566
pie
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Grouped bar charts
2+
------------------
3+
4+
The new method `~.Axes.grouped_bar()` simplifies the creation of grouped bar charts
5+
significantly. It supports different input data types (lists of datasets, dicts of
6+
datasets, data in 2D arrays, pandas DataFrames), and allows for easy customization
7+
of placement via controllable distances between bars and between bar groups.
8+
9+
Example:
10+
11+
.. plot::
12+
:include-source: true
13+
:alt: Diagram of a grouped bar chart of 3 datasets with 2 categories.
14+
15+
import matplotlib.pyplot as plt
16+
17+
categories = ['A', 'B']
18+
datasets = {
19+
'dataset 0': [1, 11],
20+
'dataset 1': [3, 13],
21+
'dataset 2': [5, 15],
22+
}
23+
24+
fig, ax = plt.subplots()
25+
ax.grouped_bar(datasets, tick_labels=categories)
26+
ax.legend()

‎galleries/examples/lines_bars_and_markers/barchart.py

Copy file name to clipboardExpand all lines: galleries/examples/lines_bars_and_markers/barchart.py
+4-12Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
# data from https://allisonhorst.github.io/palmerpenguins/
1111

1212
import matplotlib.pyplot as plt
13-
import numpy as np
1413

1514
species = ("Adelie", "Chinstrap", "Gentoo")
1615
penguin_means = {
@@ -19,22 +18,15 @@
1918
'Flipper Length': (189.95, 195.82, 217.19),
2019
}
2120

22-
x = np.arange(len(species)) # the label locations
23-
width = 0.25 # the width of the bars
24-
multiplier = 0
25-
2621
fig, ax = plt.subplots(layout='constrained')
2722

28-
for attribute, measurement in penguin_means.items():
29-
offset = width * multiplier
30-
rects = ax.bar(x + offset, measurement, width, label=attribute)
31-
ax.bar_label(rects, padding=3)
32-
multiplier += 1
23+
res = ax.grouped_bar(penguin_means, tick_labels=species, group_spacing=1)
24+
for container in res.bar_containers:
25+
ax.bar_label(container, padding=3)
3326

34-
# Add some text for labels, title and custom x-axis tick labels, etc.
27+
# Add some text for labels, title, etc.
3528
ax.set_ylabel('Length (mm)')
3629
ax.set_title('Penguin attributes by species')
37-
ax.set_xticks(x + width, species)
3830
ax.legend(loc='upper left', ncols=3)
3931
ax.set_ylim(0, 250)
4032

0 commit comments

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