python - Matplotlib 条形图图例仅显示一项
问题描述
为条形图创建图例时,仅将列表的第一个元素添加到图例中。
我试图通过分组数据框打印图例,条形图是出现的直方图
df = pd.DataFrame({'mod': biomarkers})
counts =df.groupby('mod', as_index=False)
counts.size().plot(kind='bar',width=1.0)
names = np.array(counts.describe())[:,2]
counts = np.array(counts.describe())[:,3]
plt.legend(list(names))
plt.show()
解决方案
显示来自 pd.DataFrame 的数据:
- 图例 = 列名
- x_tics = 索引
您可以将counts
Series 转换为 DataFrame 并转置。
fig, ax = plt.subplots()
df = pd.DataFrame({'mod': ['a','b','c','a','a','c']})
counts = df.groupby('mod', as_index=False)
counts_df = counts.size().to_frame().T
counts_df.plot(kind='bar', width=1.0, ax=ax)
# without plt.legend()
推荐阅读
- svg - 为什么这个 SVG 在保存为 html 时可以工作,但不能保存为 svg?
- php - PHP 计算具有复杂标准的数据
- javascript - 在页面加载时自动打开 React JS 应用程序中的模态
- node.js - tsc 的 bash 命令返回未找到
- java - java - 如何在命令行java中的-DconfigFile中有多个值?
- php - PHP - 构建前 k 个项目的排序列表
- c++ - 创建一个数组以允许存储用户输入并稍后在代码中使用
- java - 如何在所有 Android 设备上完美检测抖动水平?
- html - 如何使用 css 透视来匹配 div 以在视觉上适合 svg 笔记本电脑屏幕?
- python - mypy vs mypy-lang vs pyls-mypy python包之间的区别