如何使用matplotlib模块为水平条添加标签?

mftmpeh8  于 2022-11-15  发布在  其他
关注(0)|答案(2)|浏览(154)

由于我是新手,请告诉我应该如何为右边的每个条形添加标签。如果你能解释一下代码,那就太好了...谢谢。
这是我使用的 Dataframe :

BG_donated   Qty                            Hospital   Location    Contact
0          A-    25               Badr Al Sama Hospital      Sohar   43445995
1          A+    64                     Aster Al Raffah      Saham   58939595
2          B+    41                       Ibri Hospital       Ibri   74823847
3          B-    35                     Apollo Hospital   Hamriyah   63947392
4          O-    51   Sultan Qaboos University Hospital       Seeb   95821774
5          O+    30     Al Hayat International Hospital  Al Ghubra   44721402
6         AB-    46                  KIMS Oman Hospital    Darsait   37190481
7         AB+    41   NMC Healthcare Specialty Hospital       Ruwi   92810482

下面是水平条形图的代码:

y = df3['Qty'].sort_values()
w = df3['Hospital']
c = ['coral', 'salmon', 'indianred', 'brown', 'crimson', 'aquamarine',  
     'lightseagreen', 'slategray']
mplt.title('Analysis Report I',size = 30)
mplt.xlabel('Amount of Blood Donated',size = 15)
mplt.ylabel('Hospitals',size = 15)
mplt.barh(w,y,color = c)
mplt.show()

下面是输出:

下面是我希望它看起来的样子:

oxiaedzo

oxiaedzo1#

你是说像这样?
如果是,添加“对于i,enumerate(y)中的值:

mplt.text(values, i, '%s' %values)" to your code.
i1icjdpr

i1icjdpr2#

y = df3['Qty'].sort_values()
w = df3['Hospital']
b = df3['BG_donated']
c = ['coral', 'salmon', 'indianred', 'brown', 'crimson', 'aquamarine',  
    'lightseagreen', 'slategray']
mplt.title('Analysis Report I',size = 30)
mplt.xlabel('Amount of Blood Donated',size = 15)
mplt.ylabel('Hospitals',size = 15)
mplt.barh(w,y,color = c)

for i,values in enumerate(y):
    mplt.text(values, i, f"{b[i]}")
mplt.show()

相关问题