[图中y轴的数字排列不整齐,而x轴的单词靠得太近。我实际上是在为 COVID19 案例抓取维基百科表格,所以我不保存为 csv。我只是直接从网站上绘制它。]
我的代码也在下面
URL="https://en.wikipedia.org/wiki/COVID19_pandemic_in_Nigeria"
html=requests.get(URL)
bsObj= BeautifulSoup(html.content, 'html.parser')
states= []
cases=[]
active=[]
recovered=[]
death=[]
for items in bsObj.find("table",{"class":"wikitable
sortable"}).find_all('tr')[1:37]:
data = items.find_all(['th',{"align":"left"},'td'])
states.append(data[0].a.text)
cases.append(data[1].b.text)
active.append(data[2].text)
recovered.append(data[3].text)
death.append(data[4].text)
table= ["STATES","ACTIVE"]
tab= pd.DataFrame(list(zip(states,active)),columns=table)
tab["ACTIVE"]=tab["ACTIVE"].replace('\n','', regex=True)
x=tab["STATES"]
y=tab["ACTIVE"]
plt.cla()
plt.bar(x,y, color="green")
plt.xticks(fontsize= 5)
plt.yticks(fontsize= 8)
plt.title("PLOTTERSCA")
plt.show()