Python中的多个栏

2024-10-02 18:17:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用matplotlib绘制多个横条。 我用过:

a=(45,22,17,28)

b=(32,17,15,27)

c=(15,18,22,25)

rects1 = plt.bar(index, a, bar_width, alpha=opacity, color='b',error_kw=error_config,  label='A')

rects2 = plt.bar(index, b, bar_width,alpha=opacity, color='r', error_kw=error_config,   label='B',bottom=a)

rects4 = plt.bar(index , c, bar_width, alpha=opacity, color='y', error_kw=error_config, label='C',bottom=a+b)

我想让c超过b超过a,但bottom=a+b不起作用。。。你知道吗


Tags: alphaconfigindexmatplotlib绘制barplterror
2条回答

它失败是因为不能添加元组。您需要的是numpy阵列:

import numpy as np

a=np.array([45,22,17,28])

b=np.array([32,17,15,27])

c=np.array([15,18,22,25])

这应该是您要查找的配置:

必须用zorder>;a和>;b指定索引c,即(索引c,…,zorder=3);(索引b,…,zorder=2);(索引a,…,zorder=1)。您的代码应该如下所示:

a=(45,22,17,28)

b=(32,17,15,27)

c=(15,18,22,25)

rects1 = plt.bar(index, a, bar_width, alpha=opacity, color='b',error_kw=error_config, label='A', zorder=1)

rects2 = plt.bar(index, b, bar_width,alpha=opacity, color='r', error_kw=error_config, label='B', zorder=2)

rects4 = plt.bar(index, c, bar_width, alpha=opacity, color='y', error_kw=error_config, label='C', zorder=3)

相关问题 更多 >