Skip to content

Commit 7cf1e6b

Browse files
timhoffmstory645
andcommitted
Apply suggestions from code review
Co-authored-by: hannah <story645@gmail.com>
1 parent 3825074 commit 7cf1e6b

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

doc/users/next_whats_new/grouped_bar.rst

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Example:
1010

1111
.. plot::
1212
:include-source: true
13+
:alt: Diagram of a grouped bar chart of 3 datasets with 2 categories.
1314

1415
import matplotlib.pyplot as plt
1516

lib/matplotlib/axes/_axes.py

+15-21
Original file line numberDiff line numberDiff line change
@@ -3077,18 +3077,14 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
30773077
This function is new in v3.11, and the API is still provisional.
30783078
We may still fine-tune some aspects based on user-feedback.
30793079
3080-
This is a convenience function to plot bars for multiple datasets.
3081-
In particular, it simplifies positioning of the bars compared to individual
3082-
`~.Axes.bar` plots.
3083-
30843080
Bar plots present categorical data as a sequence of bars, one bar per category.
3085-
We call one set of such values a *dataset* and it's bars all share the same
3081+
We call one set of such values a *dataset* and its bars all share the same
30863082
color. Grouped bar plots show multiple such datasets, where the values per
30873083
category are grouped together. The category names are drawn as tick labels
30883084
below the bar groups. Each dataset has a distinct bar color, and can optionally
30893085
get a label that is used for the legend.
30903086
3091-
Here is an example call structure and the corresponding plot:
3087+
Example:
30923088
30933089
.. code-block:: python
30943090
@@ -3098,6 +3094,10 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
30983094
30993095
.. plot:: _embedded_plots/grouped_bar.py
31003096
3097+
``grouped_bar()`` is a high-level plotting function for grouped bar charts.
3098+
Use `~.Axes.bar` instead if you need finer grained control on individual bar
3099+
positions or widths.
3100+
31013101
Parameters
31023102
----------
31033103
heights : list of array-like or dict of array-like or 2D array \
@@ -3121,9 +3121,6 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
31213121
- dict of array-like: A mapping from names to datasets. Each dataset
31223122
(dict value) must have the same number of elements.
31233123
3124-
This is similar to passing a list of array-like, with the addition that
3125-
each dataset gets a name.
3126-
31273124
Example call:
31283125
31293126
.. code-block:: python
@@ -3154,21 +3151,16 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
31543151
31553152
.. code-block:: python
31563153
3157-
group_labels = ["group_A", "group_B"]
3154+
categories = ["A", "B"]
31583155
dataset_labels = ["dataset_0", "dataset_1", "dataset_2"]
31593156
array = np.random.random((2, 3))
3160-
3161-
Note that this is consistent with pandas. These two calls produce
3162-
the same bar plot structure:
3163-
3164-
.. code-block:: python
3165-
31663157
grouped_bar(array, tick_labels=categories, labels=dataset_labels)
3167-
df = pd.DataFrame(array, index=categories, columns=dataset_labels)
3168-
df.plot.bar()
31693158
31703159
- a `pandas.DataFrame`.
31713160
3161+
The index is used for the categories, the columns are used for the
3162+
datasets.
3163+
31723164
.. code-block:: python
31733165
31743166
df = pd.DataFrame(
@@ -3178,6 +3170,9 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
31783170
)
31793171
grouped_bar(df)
31803172
3173+
# is equivalent to
3174+
grouped_bar(df.to_numpy(), tick_labels=df.index, labels=df.columns)
3175+
31813176
Note that ``grouped_bar(df)`` produces a structurally equivalent plot like
31823177
``df.plot.bar()``.
31833178
@@ -3187,9 +3182,8 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
31873182
31883183
tick_labels : list of str, optional
31893184
The category labels, which are placed on ticks at the center *positions*
3190-
of the bar groups.
3191-
3192-
If not set, the axis ticks (positions and labels) are left unchanged.
3185+
of the bar groups. If not set, the axis ticks (positions and labels) are
3186+
left unchanged.
31933187
31943188
labels : list of str, optional
31953189
The labels of the datasets, i.e. the bars within one group.

0 commit comments

Comments
 (0)