如何在 matplotlib 中绘制混合 +ve 和 -ve 值的 100% 堆叠条形图?
How to draw 100% stacked bars with mixed +ve and -ve values in matplotlib?
我有一些正负值混合的数据,每个变量的绝对值之和= 100%
以下是一些示例数据:
Out01 = [79.069,-1.602,5.067,-4.241,-5.433,-4.590]
Out02 = [50.348,13.944,-15.373,6.554,5.541,8.240]
Out03 = [-8.053,0.819,-9.741,2.814,22.475,56.098]
Out04 = [-17.350,33.710,-18.510,-0.842,3.050,26.537]
Out05 = [-20.169,37.583,-20.785,-2.041,1.728,17.695]
我根据需要在 Microsoft Excel 中绘制了它们,如下所示的“100% 堆积柱形”图表:
现在我想通过matplotlib库在python中绘制类似的图表。
我该怎么做?
终于,我得到了答案,
当我跟随 the example in matplotlib page 时,它包含 bottom 关键字,它指定每个样本数据相对于前一个样本数据的上升。
p2 = plt.bar(ind, womenMeans, width, color='y', bottom=menMeans, yerr=womenStd)
例如,如果我们想像示例中那样绘制男性和女性数据,我们从得分为 20 的男性开始(在系列 G1 中),然后绘制女性,他们开始绘制 25 的值 bottom 值为 20。
为了扩展这一点,如果我们添加另一个类别,比如儿童,得分为 15,那么它应该用 bottom = 20 + 25 = 45. 等等
绘制
对于负值,我们遇到了一个问题,即它们的增长方向与正值相反。所以它应该从 bottom = 0 开始,然后是独立的正值或负值之和的最大值。
举个例子来理解这一点,如果我们想绘制如下序列:(20, 25, -15, 30, -10, -5, 17, 3, -28)
每个值的底部应如下所示 (0, 20, 0, 45, -15, -25, 75, 92, -30) 为什么?
对于20,我们只是开始绘图,所以不需要底部。
对于 25,我们需要将其提高 20。
对于-15,它是第一个负值,所以它必须画在没有底部值的轴下方,所以底部= 0
对于 30,应该提高 20 + 25 = 45
对于 -10,它应该从之前的负值(-15)以下开始
对于下一个-5,它应该从-10 + -15 = -25以下开始
等等...
def bottoms_matrix(matrix):
positives = []
negatives = []
for i, row_mat in enumerate(matrix):
tmp_p = []
tmp_n = []
for j, cell in enumerate(row_mat):
if cell >0:
tmp_p.append(cell)
tmp_n.append(0.)
else:
tmp_p.append(0.)
tmp_n.append(cell)
positives.append(tmp_p)
negatives.append(tmp_n)
# get cumulative sums
positives = positives[:-1]
negatives = negatives[:-1]
positives.insert(0, [0.] * len (matrix[0]))
negatives.insert(0, [0.] * len(matrix[0]))
tmp = swap_matrix(positives)
tmp = [list(np.cumsum(t)) for t in tmp]
positives = swap_matrix(tmp)
tmp = swap_matrix(negatives)
tmp = [list(np.cumsum(t)) for t in tmp]
negatives = swap_matrix(tmp)
final_matrix =[]
for i, row_mat in enumerate(matrix):
tmp =[]
for j, cell in enumerate(row_mat):
tmp.append(positives[i][j] if cell > 0 else negatives[i][j])
final_matrix.append(tmp)
return final_matrix
包含数据和所有辅助功能的完整示例is uploaded on my Git page.
我有一些正负值混合的数据,每个变量的绝对值之和= 100% 以下是一些示例数据:
Out01 = [79.069,-1.602,5.067,-4.241,-5.433,-4.590]
Out02 = [50.348,13.944,-15.373,6.554,5.541,8.240]
Out03 = [-8.053,0.819,-9.741,2.814,22.475,56.098]
Out04 = [-17.350,33.710,-18.510,-0.842,3.050,26.537]
Out05 = [-20.169,37.583,-20.785,-2.041,1.728,17.695]
我根据需要在 Microsoft Excel 中绘制了它们,如下所示的“100% 堆积柱形”图表:
我该怎么做?
终于,我得到了答案, 当我跟随 the example in matplotlib page 时,它包含 bottom 关键字,它指定每个样本数据相对于前一个样本数据的上升。
p2 = plt.bar(ind, womenMeans, width, color='y', bottom=menMeans, yerr=womenStd)
例如,如果我们想像示例中那样绘制男性和女性数据,我们从得分为 20 的男性开始(在系列 G1 中),然后绘制女性,他们开始绘制 25 的值 bottom 值为 20。 为了扩展这一点,如果我们添加另一个类别,比如儿童,得分为 15,那么它应该用 bottom = 20 + 25 = 45. 等等
绘制对于负值,我们遇到了一个问题,即它们的增长方向与正值相反。所以它应该从 bottom = 0 开始,然后是独立的正值或负值之和的最大值。 举个例子来理解这一点,如果我们想绘制如下序列:(20, 25, -15, 30, -10, -5, 17, 3, -28) 每个值的底部应如下所示 (0, 20, 0, 45, -15, -25, 75, 92, -30) 为什么?
对于20,我们只是开始绘图,所以不需要底部。 对于 25,我们需要将其提高 20。 对于-15,它是第一个负值,所以它必须画在没有底部值的轴下方,所以底部= 0 对于 30,应该提高 20 + 25 = 45 对于 -10,它应该从之前的负值(-15)以下开始 对于下一个-5,它应该从-10 + -15 = -25以下开始 等等...
def bottoms_matrix(matrix):
positives = []
negatives = []
for i, row_mat in enumerate(matrix):
tmp_p = []
tmp_n = []
for j, cell in enumerate(row_mat):
if cell >0:
tmp_p.append(cell)
tmp_n.append(0.)
else:
tmp_p.append(0.)
tmp_n.append(cell)
positives.append(tmp_p)
negatives.append(tmp_n)
# get cumulative sums
positives = positives[:-1]
negatives = negatives[:-1]
positives.insert(0, [0.] * len (matrix[0]))
negatives.insert(0, [0.] * len(matrix[0]))
tmp = swap_matrix(positives)
tmp = [list(np.cumsum(t)) for t in tmp]
positives = swap_matrix(tmp)
tmp = swap_matrix(negatives)
tmp = [list(np.cumsum(t)) for t in tmp]
negatives = swap_matrix(tmp)
final_matrix =[]
for i, row_mat in enumerate(matrix):
tmp =[]
for j, cell in enumerate(row_mat):
tmp.append(positives[i][j] if cell > 0 else negatives[i][j])
final_matrix.append(tmp)
return final_matrix
包含数据和所有辅助功能的完整示例is uploaded on my Git page.