Skip to content

Instantly share code, notes, and snippets.

@huseinzol05
Created January 13, 2018 09:34
Show Gist options
  • Save huseinzol05/50a0ba72131b7e245b037bb337da2315 to your computer and use it in GitHub Desktop.
Save huseinzol05/50a0ba72131b7e245b037bb337da2315 to your computer and use it in GitHub Desktop.
matplotlib nice bar plot with label
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