简体   繁体   中英

How to customize bar graph (matplotlib)?

My code:

import matplotlib.pyplot as plt
import numpy as np

f = plt.figure()
production_level = [54, 83, 21, 3] #list_of_prod
periods = [x+1 for x in range(len(production_level))] #list_of_order

plt.bar(periods, production_level, color='orange')

plt.title('Dynamic lot-size problem chart')
plt.ylabel('Units')
plt.xlabel('Periods')
plt.grid(True)

plt.show()
f.savefig("bar.png", bbox_inches='tight')

Output: 在此处输入图片说明

How can I have just whole numbers on x axis (1,2,3,4) without 0,5; 1,5; 2,5 etc.? How can add bars' value on them or above them?

Add text by using plt.text() and tweaking the coordinates (hint, hardcoding these values might not be the best idea).

Change ticks by using plt.xticks() (see also this question ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM