Skip to main content
 首页 » 编程设计

python之使用数据框 pandas (ylabel) 绘图

2025年05月04日28bhlsheji

我有以下数据框:

 frame1 = pd.DataFrame(res.items(), columns=['Region', 'Ratio']) 
 
    Region      Ratio 
  0 Midwest     0.005669 
  1 Northeast   0.008920 
  2 North       0.006374 
  3 South       0.002904 
  4 Southeast   0.001928 

我现在想用带有区域名称的 ylabel 绘制上面数据框的 (hbar),但是,我只在 y 标签上得到数字(0 到 4),如下所示:

  frame1['Ratio'].plot(y=frame1['Region'], x=frame1['Ratio'], kind="barh", align='center', color='blue') 

如何绘制每个区域的名称? 提前致谢

请您参考如下方法:

索引将是 yaxis 标签,你可以试试这个:

ax = frame1.set_index('Region').plot(kind="barh", align='center', color='blue')