python - Matplotlib:堆积条形图
问题描述
给定一个 pandas df 我想创建堆叠条形图,其中每行的所有值都堆叠在每个条形图中。我希望 xticks 是索引号,y 值是每行堆积条的总和。但是我一直无法实现它。
我得到 TypeError: only size-1 arrays can be convert to Python scalars when I try to do the plot
我试图将每一行追加到一个数组中,但我最终多次追加了相同的排列。
我在这里遵循示例:https ://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/bar_stacked.html#stacked-bar-graph
import pandas as pd
import matplotlib as plt
index C1 C2 C3
1 48692.4331 34525.0003 14020.1233
2 43206.1635 27978.9984 16572.0428
3 67398.4482 49903.4956 29856.5693
no_1 = [df["C1"] for index in df.index]
no_2 = [df["C2"] for index in df.index]
no_3 = [df["C3"] for index in df.index]
N = 3
ind = np.arange(N) # the x locations for the groups
width = 0.35 # the width of the bars: can also be len(x) sequence
p1 = plt.bar(ind, no_1, width)
p2 = plt.bar(ind, no_2, width, bottom=no_1)
p3 = plt.bar(ind, no_3, width, bottom=no_2)
plt.xticks(ind, ('no_1', 'no_2', 'no_3'))
解决方案
推荐阅读
- android - 您需要使用 OKHTTP3 以 JSON 格式发布请求 - Android
- javascript - 使用 javascript 在 div 中查找链接并转到它
- reactjs - 基于 .env 配置变量渲染基础组件
- django - 无法使用 Django 启动主管
- azure - 间歇性天蓝色 403.14
- python - 将 Groupedby 对象转换为 DataFrame 或系列,无需操作
- python - 在虚拟环境中收到提及 miniconda 的错误消息
- node.js - Google Actions SDK 对话中的第二个请求失败并显示“TypeError:标准不是函数”
- java - 如何在流中使用两个过滤器进行不同的转换
- javascript - 带有 html5 导出按钮的流星表格导出数据