Skip to content

DOC: simplify hat graph example #30459

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

Merged
merged 1 commit into from
Aug 24, 2025
Merged
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
40 changes: 16 additions & 24 deletions galleries/examples/lines_bars_and_markers/hat_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,28 @@ def hat_graph(ax, xlabels, values, group_labels):
The group labels displayed in the legend.
"""

def label_bars(heights, rects):
"""Attach a text label on top of each bar."""
for height, rect in zip(heights, rects):
ax.annotate(f'{height}',
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 4), # 4 points vertical offset.
textcoords='offset points',
ha='center', va='bottom')

values = np.asarray(values)
x = np.arange(values.shape[1])
ax.set_xticks(x, labels=xlabels)
spacing = 0.3 # spacing between hat groups
width = (1 - spacing) / values.shape[0]
heights0 = values[0]
for i, (heights, group_label) in enumerate(zip(values, group_labels)):
style = {'fill': False} if i == 0 else {'edgecolor': 'black'}
rects = ax.bar(x - spacing/2 + i * width, heights - heights0,
width, bottom=heights0, label=group_label, **style)
label_bars(heights, rects)
color_cycle_colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

# Draw the hats
bars = ax.grouped_bar(
(values - values[0]).T, bottom=values[0], tick_labels=xlabels,
labels=group_labels, edgecolor='black', group_spacing=0.8,
colors=['none'] + color_cycle_colors)

# Attach a text label on top of each bar
for bc, heights in zip(bars.bar_containers, values):
ax.bar_label(bc, heights, padding=4)

# initialise labels and a numpy array make sure you have

# Initialise labels and a numpy array make sure you have
# N labels of N number of values in the array
xlabels = ['I', 'II', 'III', 'IV', 'V']
playerA = np.array([5, 15, 22, 20, 25])
playerB = np.array([25, 32, 34, 30, 27])

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

hat_graph(ax, xlabels, [playerA, playerB], ['Player A', 'Player B'])

# Add some text for labels, title and custom x-axis tick labels, etc.
Expand All @@ -67,7 +60,6 @@ def label_bars(heights, rects):
ax.set_title('Scores by number of game and players')
ax.legend()

fig.tight_layout()
plt.show()
# %%
#
Expand All @@ -76,8 +68,8 @@ def label_bars(heights, rects):
# The use of the following functions, methods, classes and modules is shown
# in this example:
#
# - `matplotlib.axes.Axes.bar` / `matplotlib.pyplot.bar`
# - `matplotlib.axes.Axes.annotate` / `matplotlib.pyplot.annotate`
# - `matplotlib.axes.Axes.grouped_bar` / `matplotlib.pyplot.grouped_bar`
# - `matplotlib.axes.Axes.bar_label` / `matplotlib.pyplot.bar_label`
#
# .. tags::
#
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy