python - Matplotlib:如何制作 Numpy 数组中值计数的堆叠图

标签 python arrays numpy matplotlib scipy

嘿,我有以下问题。我有一个这样的数组:

 arr1=
[[4 4 4]
 [4 4 6]
 [4 3 4]
 [4 4 7]
 [4 4 3]
 [4 4 1]
 [3 4 7]
 [4 3 7]
 [4 4 5]
 [4 3 6]]

现在我想得到一个堆叠条形图(直方图),显示不同元素的数量,如下所示:

enter image description here

我的第一个方法是对元素进行 bincount,填充数组,但后来我不知道该怎么做。

arr2=
    [[0 0 0]
     [0 0 1]
     [0 0 0]
     [1 3 1]
     [9 7 2]
     [0 0 1]
     [0 0 2]
     [0 0 3]]

最佳答案

这是输出

enter image description here

以及生成相同内容的代码

import numpy as np
import pandas as pd
data = np.array([[4, 4, 4],
 [4, 4, 6],
 [4, 3, 4],
 [4, 4, 7],
 [4, 4, 3],
 [4, 4, 1],
 [3, 4, 7],
 [4, 3, 7],
 [4, 4, 5],
 [4, 3, 6]])

columns = ['Col1', 'Col2', 'Col3']
df = pd.DataFrame(data, columns=columns)
out = {}
for column in columns:
    out[column] = pd.value_counts(df[column])

uniq_df = pd.DataFrame(out).fillna(0)

uniq_df.T.plot(kind="bar", stacked=True)

关于python - Matplotlib:如何制作 Numpy 数组中值计数的堆叠图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44924711/

相关文章:

javascript - 如何在Javascript中通过多个参数对非常大的数据数组进行排序

python - 如何以向量化形式编写这个 numpy 代码?

python - 循环元素时如何解决 StaleElementReferenceException (Selenium)

python - Tkinter .after 模块只是延迟了 GUI 的打开

python - 需要帮助在 pygame 中创建按钮

python - 混合数据类型的转换器

javascript - NodeJS - 从多级对象数组中提取数据

arrays - Powershell Regex 使用 -Split() 手动创建 $Args[*] 数组

python - 使用 GNU screen 时的权限问题

python - numpy.linalg.eig 如何决定返回特征值的顺序?