Dataframe 如何在matplotlib中增加条间距和条宽度

Dataframe 如何在matplotlib中增加条间距和条宽度,dataframe,matplotlib,web-scraping,bar-chart,wikipedia,Dataframe,Matplotlib,Web Scraping,Bar Chart,Wikipedia,我正在从维基百科网站上直接抓取一张维基百科表格,并绘制表格。我想增加酒吧的宽度,增加酒吧之间的空间,使所有酒吧可见。请问我该怎么办?下面是我的代码 使用及 .read\u html将从网站读取所有表标记,并返回数据帧列表 barh将使水平条代替垂直条,如果有很多条,这很有用 如果需要,使绘图更长。在这种情况下,(16.0,10.0),增加10 我建议对x使用对数刻度,因为与Kogi 这不会在条之间留出更多的空间,但随着尺寸和水平条的增加,格式化的绘图更加清晰 .iloc[:36,:5]从数据

我正在从维基百科网站上直接抓取一张维基百科表格,并绘制表格。我想增加酒吧的宽度,增加酒吧之间的空间,使所有酒吧可见。请问我该怎么办?下面是我的代码

使用及
  • .read\u html
    将从网站读取所有表标记,并返回数据帧列表
  • barh
    将使水平条代替垂直条,如果有很多条,这很有用
  • 如果需要,使绘图更长。在这种情况下,
    (16.0,10.0)
    ,增加
    10
  • 我建议对x使用对数刻度,因为与
    Kogi
  • 这不会在条之间留出更多的空间,但随着尺寸和水平条的增加,格式化的绘图更加清晰
  • .iloc[:36,:5]
    从数据帧中删除一些不需要的列和行
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
#网址
url='1〕https://en.wikipedia.org/wiki/COVID-19_pandemic_in_Nigeria'
#创建数据帧列表
dataframe_list=pd.read_html(url)#这是url上所有表作为数据帧的列表
#从列表中获取数据帧
df=dataframe_list[2]。iloc[:36,:5]#您希望数据帧位于索引2处
#将“-”替换为0
df.replace('-',0,原地=真)
#设置为int
对于df.列[1:]中的列:
df[col]=df[col].astype('int')
#画单杠
plt.rcParams['figure.figsize']=(16.0,10.0)
plt.style.use('ggplot'))
p=plt.barh(宽度='Cases',y='State',数据=df,颜色='purple')
plt.xscale('log')
plt.xlabel(“案例数”)
plt.show()

df中绘制所有数据
df.set_索引('State',inplace=True)
plt.图(figsize=(14,14))
df.plot.barh()
plt.xscale('log')
plt.show()

4个子地块
  • 状态
    作为索引
plt.图(figsize=(14,14))
对于i,枚举中的列(df.columns,1):
plt.子地块(2,2,i)
df[col].plot.barh(label=col,color='green')
plt.xscale('log')
plt.legend()
plt.紧_布局()
plt.show()
#########scrapping#########
html= requests.get("https://en.wikipedia.org/wiki/COVID-19_pandemic_in_Nigeria")
bsObj= BeautifulSoup(html.content, 'html.parser')
states= []
cases=[]

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)
  
 ########Dataframe#########
table= ["STATES","CASES"]
tab= pd.DataFrame(list(zip(states,cases)),columns=table)
tab["CASES"]=tab["CASES"].replace('\n','', regex=True)
tab["CASES"]=tab["CASES"].replace(',','', regex=True)
tab['CASES'] = pd.to_numeric(tab['CASES'], errors='coerce')
tab["CASES"]=tab["CASES"].fillna(0)
tab["CASES"] = tab["CASES"].values.astype(int)

#######matplotlib########
x=tab["STATES"]
y=tab["CASES"]
plt.cla()
plt.locator_params(axis='y', nbins=len(y)/4)
plt.bar(x,y, color="blue")
plt.xticks(fontsize= 8,rotation='vertical')
plt.yticks(fontsize= 8)
plt.show()