python - 使用 python/pandas 将数据标准化并绘制为堆积条形图

标签 python pandas matplotlib plot normalize

我有以下数据框:

    land_cover  canopy_cat  count  tc_density_cor
0           20           1     56       35.760967
1           20           2     28       35.760967
2           20           3     11       35.760967
3           20           4      9       35.760967
4           20           5      4       35.760967
5           20           6      3       35.760967
6           20           7      3       35.760967
7           20           8      1       35.760967
8           20           9      4       35.760967
9           20          10      6       35.760967
10          20          11      2       35.760967
11          30           1    194       17.408260
12          30           2     86       17.408260
13          30           3     55       17.408260
14          30           4     36       17.408260
15          30           5     21       17.408260
16          30           6     15       17.408260
17          30           7      9       17.408260
18          30           8      6       17.408260
19          30           9     19       17.408260
20          30          10     14       17.408260
21          30          11      9       17.408260
22          40           1    106       17.458283
23          40           2     45       17.458283
24          40           3     19       17.458283
25          40           4     14       17.458283
26          40           5      9       17.458283
27          40           6      8       17.458283
28          40           7      5       17.458283
29          40           8      5       17.458283
30          40           9      8       17.458283
31          40          10     12       17.458283
32          40          11      3       17.458283


我想将我的数据绘制为堆积条形图:
x 轴 = land_cover
y 轴 = 每个 canopy_cat 的计数

我认为枢轴函数就是我正在寻找的。然而,在我想标准化每个 land_cover 相对于“tc_密度_cor”的“计数”列之前。
例如,land_cover=20 的“计数”总和 = 127。
127/35.76 = 56/x --> 新值为:15.76

我怎样才能做到这一点?? :)

最佳答案

我认为你需要:

df['Count Per Canopy Cat'] = (df['count'] * df['tc_density_cor'] / 
                              df.groupby('land_cover')['count'].transform(sum))

df.pivot('land_cover',
         'canopy_cat',
         'Count Per Canopy Cat')\
  .plot.bar(stacked=True, figsize=(15,8))

图表:

enter image description here

关于python - 使用 python/pandas 将数据标准化并绘制为堆积条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51747539/

相关文章:

python - Pandas-根据重叠时间段分割数据集

python 和 Pandas : Convert series of datetime-values to consecutive day-indices

python - python动态规划表的绘制方法

python - 面向对象的 matplotlib 使用神秘地生成没有 mustache 或中线的箱线图

python - 框架的tkinter滚动条

python - 没有名为 urls 的模块/django + mod_wsgi 没有响应

Python numpy recarray : Can one obtain a view into different fields using pointer arithmetic?

python - 在 Python 中绘制(纬度、经度、值)元组列表?

python - Pandas 多个 excel 表中的 dict 列表

python - matplotlib 无法在 Windows 10 上加载 ft2font