|
1 | 1 | """
|
2 |
| -=============================== |
3 |
| -Hat Graph with labels |
4 |
| -=============================== |
5 |
| -This example shows a how to create a hat graph |
6 |
| -and how to annotate with labels. |
7 |
| -Refer (https://doi.org/10.1186/s41235-019-0182-3) |
8 |
| -to know more about hat graph |
| 2 | +========= |
| 3 | +Hat graph |
| 4 | +========= |
| 5 | +This example shows how to create a `hat graph`_ and how to annotate it with |
| 6 | +labels. |
| 7 | +
|
| 8 | +.. _hat graph: https://doi.org/10.1186/s41235-019-0182-3 |
9 | 9 | """
|
10 | 10 | import matplotlib
|
11 | 11 | import numpy as np
|
12 | 12 | import matplotlib.pyplot as plt
|
| 13 | + |
| 14 | + |
| 15 | +def hat_graph(ax, xlabels, values, group_labels): |
| 16 | + """ |
| 17 | + Create a hat graph. |
| 18 | +
|
| 19 | + Parameters |
| 20 | + ---------- |
| 21 | + ax : matplotlib.axes.Axes |
| 22 | + The Axes to plot into. |
| 23 | + xlabels : list of str |
| 24 | + The category names to be displayed on the x-axis. |
| 25 | + values : array-like (M, N) |
| 26 | + The data values. |
| 27 | + Rows are the groups (len(group_labels) == M). |
| 28 | + Columns are the categories (len(xlabels) == N). |
| 29 | + group_labels : list of str |
| 30 | + The group labels displayed in the legend. |
| 31 | + """ |
| 32 | + |
| 33 | + def label_bars(heights, rects): |
| 34 | + """Attach a text label on top of each bar.""" |
| 35 | + for height, rect in zip(heights, rects): |
| 36 | + ax.annotate(f'{height}', |
| 37 | + xy=(rect.get_x() + rect.get_width() / 2, height), |
| 38 | + xytext=(0, 4), # 4 points vertical offset. |
| 39 | + textcoords='offset points', |
| 40 | + ha='center', va='bottom') |
| 41 | + |
| 42 | + values = np.asarray(values) |
| 43 | + x = np.arange(values.shape[1]) |
| 44 | + ax.set_xticks(x) |
| 45 | + ax.set_xticklabels(xlabels) |
| 46 | + spacing = 0.3 # spacing between hat groups |
| 47 | + width = (1 - spacing) / values.shape[0] |
| 48 | + heights0 = values[0] |
| 49 | + for i, (heights, group_label) in enumerate(zip(values, group_labels)): |
| 50 | + style = {'fill': False} if i == 0 else {'edgecolor': 'black'} |
| 51 | + rects = ax.bar(x - spacing/2 + i * width, heights - heights0, |
| 52 | + width, bottom=heights0, label=group_label, **style) |
| 53 | + label_bars(heights, rects) |
| 54 | + |
| 55 | + |
13 | 56 | # initialise labels and a numpy array make sure you have
|
14 | 57 | # N labels of N number of values in the array
|
15 |
| -labels = ['I', 'II', 'III', 'IV', 'V'] |
| 58 | +xlabels = ['I', 'II', 'III', 'IV', 'V'] |
16 | 59 | playerA = np.array([5, 15, 22, 20, 25])
|
17 | 60 | playerB = np.array([25, 32, 34, 30, 27])
|
18 |
| -x = np.arange(len(labels)) |
19 |
| -width = 0.35 |
| 61 | + |
20 | 62 | fig, ax = plt.subplots()
|
21 |
| -rects1 = ax.bar(x - width/2, np.zeros_like(playerA), width, |
22 |
| - bottom=playerA, label='Player A', fill=False) |
23 |
| -rects2 = ax.bar(x + width/2, playerB - playerA, width, |
24 |
| - bottom=playerA, label='Player B', edgecolor='black') |
| 63 | +hat_graph(ax, xlabels, [playerA, playerB], ['Player A', 'Player B']) |
25 | 64 |
|
26 | 65 | # Add some text for labels, title and custom x-axis tick labels, etc.
|
27 |
| -ax.set_ylim(0, 60) |
| 66 | +ax.set_xlabel('Games') |
28 | 67 | ax.set_ylabel('Score')
|
29 |
| -ax.set_title('Scores by number of game and Players') |
30 |
| -ax.set_xticks(x) |
31 |
| -ax.set_xticklabels(labels) |
| 68 | +ax.set_ylim(0, 60) |
| 69 | +ax.set_title('Scores by number of game and players') |
32 | 70 | ax.legend()
|
33 |
| -ax.set_xlabel('Games') |
34 |
| - |
35 | 71 |
|
36 |
| -def Label(heights, rects): |
37 |
| - """Attach a text label on top of each bar.""" |
38 |
| - i = 0 |
39 |
| - for rect in rects: |
40 |
| - height = int(heights[i]) |
41 |
| - i += 1 |
42 |
| - ax.annotate('{}'.format(height), |
43 |
| - xy=(rect.get_x() + rect.get_width() / 2, height), |
44 |
| - xytext=(0, 4), # 4 points vertical offset. |
45 |
| - textcoords="offset points", |
46 |
| - ha='center', va='bottom') |
47 |
| -Label(playerA, rects1) |
48 |
| -Label(playerB, rects2) |
49 | 72 | fig.tight_layout()
|
50 | 73 | plt.show()
|
51 | 74 | #############################################################################
|
|
0 commit comments