diff --git a/galleries/examples/lines_bars_and_markers/hat_graph.py b/galleries/examples/lines_bars_and_markers/hat_graph.py index 0fb611bc9262..25e5f0b1ead3 100644 --- a/galleries/examples/lines_bars_and_markers/hat_graph.py +++ b/galleries/examples/lines_bars_and_markers/hat_graph.py @@ -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. @@ -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() # %% # @@ -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:: # 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