如何从两个列表中创建堆叠条形图:考虑一个是集群,另一个是标志

How to create a stacked bar chart out of two lists: considering one is as cluster, and other one is flag

假设我有以下代码片段:

x = [1,1,1,1,2,3,3,3]  
y = [1,1,0,0,1,1,1,0]

import matplotlib.pyplot as plt from collections  
import Counter    
freqs = Counter(x)
plt.bar(freqs.keys(), freqs.values(), width=0.5)
plt.xticks(list(freqs.keys()))

我想通过按以下 y 值对条形图进行着色来提供堆叠条形图,如下所示:

如何将 y 个值整合到此条形图中?

最直接的堆叠条形图可以使用 Matplotlib 实现。

import matplotlib.pyplot as plt

dataset= [(1, 1), (1, 1), (1, 0), (1, 0), (2, 1), (3, 1), (3, 1), (3, 0)]

y_1 = [len([1 for data in dataset if (data[0] == cat) and (data[1] == 1)])for cat in [1,2,3]]
y_0 = [len([1 for data in dataset if (data[0] == cat) and (data[1] == 0)])for cat in [1,2,3]]

plt.bar(x=['1','2','3'], height=y_1, label='1', bottom=y_0)
plt.bar(x=['1','2','3'], height=y_0, label='0')
plt.legend()
plt.xlabel('Category')
plt.ylabel('Frequency')
plt.show()

或者如果您熟悉 Pandas,您也可以使用内置的绘图功能,它给出了类似的绘图:

import pandas as pd

dataset= [(1, 1), (1, 1), (1, 0), (1, 0), (2, 1), (3, 1), (3, 1), (3, 0)]

x = [tup[0] for tup in dataset]
y = [tup[1] for tup in dataset]

df = pd.DataFrame({'x':x, 'y':y, 'freq_y':0})

ax = df.groupby(['x','y']).count().unstack(1).plot(y='freq_y',kind='bar', stacked=True)
ax.set_ylabel('Frequency')
ax.set_xlabel('Category')
plt.show()