0

[the numbers in yaxis in the plot is not arranged in order while the word in x axis are too close. I am actually scraping wikipedia table for COVID19 cases, so i dont save as csv. i am only ploting it directly from the website.]

enter image description here

my code also below

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()
Temitope
  • 27
  • 5

2 Answers2

0

It's hard to say without the data, but you can try this to sort the values on y axis:

y.sort()
plt.bar(x,y, color="green")
Qasim Khan
  • 154
  • 10
0

Barcharts are plotted in the order they are presented. In this case, there is no need to create a dataframe. Try the following:

x=[s.replace('\n', '') for s in  states]
y=np.array(active)

order = np.argsort(y)

xNew = [x[i] for i in order]
yNew = y[order]

plt.cla()
plt.bar(xNew,yNew, color="green")
plt.xticks(fontsize= 5, rotation=90)
plt.yticks(fontsize= 8)
plt.title("PLOTTERSCA")
plt.show()

Here, we have reordered everything based upon the y values. Also, the xticks have been rotated so that they are easier to see ...

ssm
  • 5,277
  • 1
  • 24
  • 42
  • my post has been editted. i am actually trying to plot the table directly from the website without saving the table as csv first. – Temitope Jun 21 '20 at 18:43
  • still, the same logic should apply. Have you actually tried the suggestion? – ssm Jun 22 '20 at 01:52