Created
January 13, 2018 09:34
-
-
Save huseinzol05/50a0ba72131b7e245b037bb337da2315 to your computer and use it in GitHub Desktop.
matplotlib nice bar plot with label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = np.random.randint(0, 100, size=(100)) | |
rects1=plt.bar(np.arange(x.shape[0]), x) | |
def autolabel(rects): | |
(y_bottom, y_top) = plt.ylim() | |
y_height = y_top - y_bottom | |
for rect in rects: | |
height = rect.get_height() | |
p_height = (height / y_height) | |
if p_height > 0.95: | |
label_position = height - (y_height * 0.05) | |
else: | |
label_position = height + (y_height * 0.01) | |
plt.text(rect.get_x() + rect.get_width()/2., label_position,'%f' % height,ha='center', va='bottom') | |
autolabel(rects1) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment