pythonmatplotlibgraphyaxis

Bar Graph not showing 0


The y-axis of my bar graph isn't starting from 0. It is taking the minimum of the three values to be displayed as the starting point. I want it to go from zero to fifteen

data = {'Average Time of 1st Instance':average_value1, 'Average Time of 2nd Instance':average_value2, 'Average Time of 3rd Instance':average_value3}  
instances = list(data.keys())  
times = list(data.values())  
fig = plt.figure(figsize = (10, 5))
plt.bar(instances, times, color ='blue', width = 0.4)
plt.xlabel('Instance of ++HSS')
plt.ylabel('Time in Seconds')
plt.title('Time Taken to run the Three Instances of ++HSS')`  
plt.show()

This is the graph I'm getting:

Graph

Setting the y axis limits using plt.ylim(0, 15) is also not working

Data:
average_value1 = 8.88
average_value2 = 11.86
average_value3 = 11.36


Solution

  • The problem is that your average_value values are strings rather than numbers. If you change the line:

    times = list(data.values())
    

    to instead be:

    times = [float(d) for d in data.values()]
    

    to explicitly convert the values to floating point numbers you should get what you expect.

    Alternatively, convert them when you create the dictionary, i.e.,

    data = {
        'Average Time of 1st Instance': float(average_value1),
        'Average Time of 2nd Instance': float(average_value2),
        'Average Time of 3rd Instance': float(average_value3)
    }