Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 05ae526

Browse files
committed
Improve hat graph example
1 parent 1ed5c91 commit 05ae526

File tree

1 file changed

+56
-33
lines changed

1 file changed

+56
-33
lines changed

examples/lines_bars_and_markers/hat_graph.py

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,74 @@
11
"""
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
99
"""
1010
import matplotlib
1111
import numpy as np
1212
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+
1356
# initialise labels and a numpy array make sure you have
1457
# N labels of N number of values in the array
15-
labels = ['I', 'II', 'III', 'IV', 'V']
58+
xlabels = ['I', 'II', 'III', 'IV', 'V']
1659
playerA = np.array([5, 15, 22, 20, 25])
1760
playerB = np.array([25, 32, 34, 30, 27])
18-
x = np.arange(len(labels))
19-
width = 0.35
61+
2062
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'])
2564

2665
# Add some text for labels, title and custom x-axis tick labels, etc.
27-
ax.set_ylim(0, 60)
66+
ax.set_xlabel('Games')
2867
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')
3270
ax.legend()
33-
ax.set_xlabel('Games')
34-
3571

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)
4972
fig.tight_layout()
5073
plt.show()
5174
#############################################################################

0 commit comments

Comments
 (0)