[英]Matplotlib: How to make a stacked plot of count of values in a Numpy array
Hey I have the following problem. 嘿,我有以下问题。 I have a array like this:
我有一个像这样的数组:
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]]
Now i want to get a stacked bar plot(histogram), that shows the number of different elements,, like this: 现在,我想获得一个堆积的条形图(直方图),它显示了不同元素的数量,如下所示:
My frist approach was to bincount the elements, fill the arrays up, but then i dont know what to do. 我的第一种方法是对元素进行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]]
Here is the output 这是输出
and the code to generate the same 并生成相同的代码
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)
Adding an Altair based answer as well. 也添加基于Altair的答案。
import numpy as np
import pandas as pd
from altair import *
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)
df = df.T.stack().reset_index(level=[0,1])
df.columns = ['Col','RowNum','Value']
Chart(df).mark_bar().encode(y='count(*)', x='Col:N', color='Value:N')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.