如何在python中制作堆叠条形图

问题描述 投票:-1回答:1

嗨,我在制作堆积条形图时遇到麻烦,这是我的df

In[]top_10_medals_breakdown = pd.DataFrame()
    top_10_medals_breakdown = top_10_medals_breakdown.append(d)
    top_10_medals_breakdown
Out[]
    Noc Medal   Count
342 USA Bronze  1358
343 USA Gold    2638
344 USA Silver  1641
336 URS Bronze  689
337 URS Gold    1082
338 URS Silver  732
124 GER Bronze  746
125 GER Gold    745
126 GER Silver  674
115 GBR Bronze  651
116 GBR Gold    678
117 GBR Silver  739
108 FRA Bronze  666
109 FRA Gold    501
110 FRA Silver  610
167 ITA Bronze  531
168 ITA Gold    575
169 ITA Silver  531
296 SWE Bronze  535
297 SWE Gold    479
298 SWE Silver  522
48  CAN Bronze  451
49  CAN Gold    463
50  CAN Silver  438
14  AUS Bronze  517
15  AUS Gold    348
16  AUS Silver  455
142 HUN Bronze  371
143 HUN Gold    432
144 HUN Silver  332

这是我尝试的栏目,仅读取金牌数

plt.bar(top_10_medals_breakdown['Noc'], top_10_medals_breakdown['Count'], color='b')

It only counts the gold medals

所以tl:dr,我想制作一个堆叠的条形图,以计算每个国家的奖牌数

python pandas matplotlib seaborn
1个回答
0
投票
import pandas as pd import numpy as np import io data = ''' Noc Medal Count 342 USA Bronze 1358 343 USA Gold 2638 344 USA Silver 1641 336 URS Bronze 689 337 URS Gold 1082 338 URS Silver 732 124 GER Bronze 746 125 GER Gold 745 126 GER Silver 674 115 GBR Bronze 651 116 GBR Gold 678 117 GBR Silver 739 108 FRA Bronze 666 109 FRA Gold 501 110 FRA Silver 610 167 ITA Bronze 531 168 ITA Gold 575 169 ITA Silver 531 296 SWE Bronze 535 297 SWE Gold 479 298 SWE Silver 522 48 CAN Bronze 451 49 CAN Gold 463 50 CAN Silver 438 14 AUS Bronze 517 15 AUS Gold 348 16 AUS Silver 455 142 HUN Bronze 371 143 HUN Gold 432 144 HUN Silver 332 ''' df = pd.read_csv(io.StringIO(data), sep='\s+') df3 = df.pivot(columns='Medal', index='Noc', values='Count') df3.sort_values('Gold', ascending=False, inplace=True) df3 = df3.iloc[:,[1,2,0]] df3 Medal Bronze Gold Silver Noc USA 1358 2638 1641 URS 689 1082 732 GER 746 745 674 GBR 651 678 739 ITA 531 575 531 FRA 666 501 610 SWE 535 479 522 CAN 451 463 438 HUN 371 432 332 AUS 517 348 455

enter image description here

热门问题
推荐问题
最新问题