小能豆

Bar Graph not showing 0

py

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


阅读 66

收藏
2023-12-12

共1个答案

小能豆

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.

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.

2023-12-12