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:
Setting the y axis limits using plt.ylim(0, 15) is also not working
plt.ylim(0, 15)
Data: average_value1 = 8.88 average_value2 = 11.86 average_value3 = 11.36
average_value1 = 8.88
average_value2 = 11.86
average_value3 = 11.36
It seems like your issue is related to the fact that Matplotlib automatically adjusts the y-axis limits based on the data values. To ensure that the y-axis starts from zero, you can set the bottom parameter of the bar function to zero.
bottom
bar
Here’s how you can modify your code:
import matplotlib.pyplot as plt data = {'Average Time of 1st Instance': 8.88, 'Average Time of 2nd Instance': 11.86, 'Average Time of 3rd Instance': 11.36} instances = list(data.keys()) times = list(data.values()) fig = plt.figure(figsize=(10, 5)) plt.bar(instances, times, color='blue', width=0.4, bottom=0) # Set bottom parameter to 0 plt.xlabel('Instance of ++HSS') plt.ylabel('Time in Seconds') plt.title('Time Taken to run the Three Instances of ++HSS') plt.show()
Setting bottom=0 explicitly tells Matplotlib to start the bars from zero on the y-axis. This should ensure that your y-axis starts from zero in the bar graph.
bottom=0