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

ENH: Add grouped_bar() method #28560

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
wants to merge 6 commits into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions 15 doc/_embedded_plots/grouped_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import matplotlib.pyplot as plt

categories = ['A', 'B']
data0 = [1.0, 3.0]
data1 = [1.4, 3.4]
data2 = [1.8, 3.8]
timhoffm marked this conversation as resolved.
Show resolved Hide resolved

fig, ax = plt.subplots(figsize=(4, 2.2))
ax.grouped_bar(
[data0, data1, data2],
tick_labels=categories,
labels=['dataset 0', 'dataset 1', 'dataset 2'],
colors=['#1f77b4', '#58a1cf', '#abd0e6'],
)
ax.legend()
1 change: 1 addition & 0 deletions 1 doc/api/axes_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Basic
Axes.bar
Axes.barh
Axes.bar_label
Axes.grouped_bar

Axes.stem
Axes.eventplot
Expand Down
1 change: 1 addition & 0 deletions 1 doc/api/pyplot_summary.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Basic
bar
barh
bar_label
grouped_bar
stem
eventplot
pie
Expand Down
26 changes: 26 additions & 0 deletions 26 doc/users/next_whats_new/grouped_bar.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Grouped bar charts
------------------

The new method `~.Axes.grouped_bar()` simplifies the creation of grouped bar charts
significantly. It supports different input data types (lists of datasets, dicts of
datasets, data in 2D arrays, pandas DataFrames), and allows for easy customization
of placement via controllable distances between bars and between bar groups.

Example:

.. plot::
:include-source: true
:alt: Diagram of a grouped bar chart of 3 datasets with 2 categories.

timhoffm marked this conversation as resolved.
Show resolved Hide resolved
import matplotlib.pyplot as plt

categories = ['A', 'B']
datasets = {
'dataset 0': [1, 11],
'dataset 1': [3, 13],
'dataset 2': [5, 15],
}

fig, ax = plt.subplots()
ax.grouped_bar(datasets, tick_labels=categories)
ax.legend()
16 changes: 4 additions & 12 deletions 16 galleries/examples/lines_bars_and_markers/barchart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
# data from https://allisonhorst.github.io/palmerpenguins/

import matplotlib.pyplot as plt
import numpy as np

species = ("Adelie", "Chinstrap", "Gentoo")
penguin_means = {
Expand All @@ -19,22 +18,15 @@
'Flipper Length': (189.95, 195.82, 217.19),
}

x = np.arange(len(species)) # the label locations
width = 0.25 # the width of the bars
multiplier = 0

fig, ax = plt.subplots(layout='constrained')

for attribute, measurement in penguin_means.items():
offset = width * multiplier
rects = ax.bar(x + offset, measurement, width, label=attribute)
ax.bar_label(rects, padding=3)
multiplier += 1
res = ax.grouped_bar(penguin_means, tick_labels=species, group_spacing=1)
for container in res.bar_containers:
ax.bar_label(container, padding=3)

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

Expand Down
Loading
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.