首页 > 解决方案 > 如何使该图的 y 轴从零开始?

问题描述

url = "https://www.worldometers.info/coronavirus/"
response = requests.get(url)
data = response.text
soup = BeautifulSoup(data, "html.parser")    

'''Since there are no unary operators supported between 'counter' and the variable 'i',
I must declare another variable 'counter' and set it equal to zero, with the value
of counter being updated by 1 each time.'''
total_cases = []
counter = 0;
global_case_information = soup.find_all("div", {"class":"maincounter-number"})
for cases in global_case_information:
    if(counter == 13):
        break
    else:
        total_cases.append(cases.text)
        counter += 1    
global_cases = {"Total Deaths":total_cases[1], "Total Recovered":total_cases[2]}

covid_figure = plt.figure()
plt.rcParams["figure.figsize"] = [40, 40]
keys_array = np.arange(len(global_cases.keys()))

axes = plt.gca()
plt.xticks(keys_array, global_cases.keys())
plt.ylabel("Number Of People Affected(Globally)")
plt.title("Global Covid-19 Statistics")    
plt.bar(keys_array, global_cases.values())    
plt.show()

好的,所以我试图让 y 轴从零开始,并且相对偏移量为 50000 个单位。但是,事实并非如此,我什至尝试执行以下操作:

plt.yticks(np.arange(0, 300000, 50000))

但这行不通。

标签: pythonnumpymatplotlib

解决方案


请在 plt.show() 之前设置以下命令

plt.set_ylim(ymin=0)

我希望你问这个。请告诉我。


推荐阅读