简体   繁体   English

在熊猫中创建堆叠的条形图

[英]Create stacked bar plot in pandas

I am plotting the foll . 我绘制foll dataframe using pandas: 使用熊猫的数据框:

╔═════════════════════════════════════════════════════════════════════════╗
║                       country_name              1960s      1970s        ║
╠═════════════════════════════════════════════════════════════════════════╣
║ functional_crop_type                                                    ║
║ C3annual              United States of America  48.226957  42.384590    ║
║ C3perennial           United States of America   1.377634   1.377971    ║
║ C4annual              United States of America  32.526747  32.833662    ║
║ C4perennial           United States of America   0.265062   0.289005    ║
║ N-fixing              United States of America  17.603600  23.114773    ║
╚═════════════════════════════════════════════════════════════════════════╝   
╔════════════════════════════════════════════════════════════════════╗
║                       1980s      1990s      2000s      2010s       ║
╠════════════════════════════════════════════════════════════════════╣
║ functional_crop_type                                               ║
║ C3annual              41.184368  39.085943  32.821374  28.767468   ║
║ C3perennial            1.413647   1.561404   1.657869   1.691046   ║
║ C4annual              31.260082  32.089138  33.705184  36.815154   ║
║ C4perennial            0.303626   0.367331   0.386495   0.359842   ║
║ N-fixing              25.838277  26.896184  31.429078  32.366490   ║
╚════════════════════════════════════════════════════════════════════╝

Using the foll . 使用foll command: 命令:

out_dec_df = out_dec_df.set_index('functional_crop_type')
out_dec_df.plot(kind='bar',stacked=True)

However, I want the x-axis to be '1960s', '1970s'...'2010s' and the stacked bar plot to be composed of the functional crop types. 但是,我希望x-axis为“ 1960年代”,“ 1970年代” ...“ 2010年代”,并且堆积条形图由功能性作物类型组成。 How do I do this? 我该怎么做呢?

在此处输入图片说明

尝试使用转置。

out_dec_df.drop('country_name', axis=1).T.plot(kind='bar',stacked=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM