Drew Bennett
Drew Bennett

Reputation: 483

Lines in a bar chart Matplotlib

enter image description hereMy goal is to have a bar chart represent allocated hours. The allocated hours should be represented as tot_hrs above the sched_hrs. So, a line at 7 should be right above bar of 1 at 'Blairstone FL'. However, the plot comes out with three lines 7, 8, and 9 all above 'Blairstone FL', 'Bonifay FL', and 'Calhoun FL'. The main issue exists the code below for tt in range(len(service_areas)):. Any help is much appreciated.

sched_hrs=[1, 2, 3]
tot_hrs=[7, 8, 9]
columnstart = -0.5
columnend = 0.5
service_areas = ['Blairstone FL', 'Bonifay FL', 'Calhoun FL']

plt.figure(figsize=(len(service_areas) + 4.5, 4)).canvas.set_window_title('Click')
ind = np.arange(len(service_areas))
width = 0.18
plt.bar(ind, sched_hrs,width, label='Scheduled Hours', color='green', align="center")
for tt in range(len(service_areas)):
    plt.plot([columnstart, columnend], [tot_hrs, tot_hrs], color='#228B22', linestyle='-', linewidth=2)
    columnstart +=1
    columnend +=1
plt.xticks(ind, service_areas, rotation=10)
plt.yticks(np.arange(0, max(1, max(tot_hrs) + 2), 1))
plt.title("Allocated Hours")
plt.xlabel("Areas")
plt.ylabel("Hours")
plt.legend()
plt.show() 

Upvotes: 1

Views: 515

Answers (1)

Anthony Kong
Anthony Kong

Reputation: 40874

Assuming this is what you want to achieve:

enter image description here

Then you should plot the line like this:

plt.bar(ind, 0.1, width, bottom=tot_hrs, color='green', align="center")

to replace this block of code:

for tt in range(len(service_areas)):
    plt.plot([columnstart, columnend], [tot_hrs, tot_hrs], color='#228B22', linestyle='-', linewidth=2)
    columnstart +=1
    columnend +=1

Upvotes: 3

Related Questions