-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbar_plot_with_data_labels.py
More file actions
42 lines (31 loc) · 949 Bytes
/
bar_plot_with_data_labels.py
File metadata and controls
42 lines (31 loc) · 949 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 2 00:05:33 2019
Bar plot with data labels
@author: alberto
"""
import numpy as np
import matplotlib.pyplot as plt
n = 12
X = np.arange(n)
Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')
plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
# Label the blue bars with data
# Fine-tune manually position of test by trial and error
for x, y in zip(X, Y1):
plt.text(x + 0.1, y + 0.05, '%.2f' % y,
ha='center', va='bottom')
# Label the red bars with data
for x, y in zip(X, Y2):
plt.text(x + 0.1, - y - 0.15, '%.2f' % y,
ha='center', va='bottom')
# Set y limits
plt.ylim(-1.25, +1.25)
# Hide axis
plt.axis('off')
# Save figure using 72 dots per inch
plt.savefig('bar_plot_with_data_labels.png', dpi=72)
plt.show()