堆叠的matplotlib栏中的替代颜色 [英] Alternate colors in stacked matplotlib bar

查看:64
本文介绍了堆叠的matplotlib栏中的替代颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在matplotlib中生成堆积的条形图(在Windows 7上使用Python 2.7).

I'm generating a stacked bar chart in matplotlib (using Python 2.7 on Windows 7).

因为我想用它来成对比较来自 2 个数据集的数据,所以我想为每个第二条使用不同的颜色.谁能告诉我如何实现这一目标?

Since I want to use it to pairwise compare data from 2 data-sets, I would like to use different colors for every 2nd bar. Can anyone tell me how to achieve this?

我的条形图基本上是这样的:

My bar chart looks basically like this:

import numpy
import matplotlib.pyplot as plt


fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

IDs = ["1","A","2","B","3","C","4","D","5","E"]
N = len(IDs)

property1 = numpy.array([1,3,4,2,3,5,6,7,3,2])
property2 = numpy.array(range(10))
property3 = numpy.array(range(10,0,-1))

ind = numpy.arange(N)
width = 0.8

p1 = ax1.bar(ind, property1, width, color='red')
p2 = ax1.bar(ind, property2, width, color='blue', bottom=property1)
p3 = ax1.bar(ind, property3, width, color='green', bottom=property1 + property2)
plt.xticks(ind+width/2., IDs )

plt.show()
plt.close()

所以我想对标有字母的条使用一种配色方案,为那些标有数字的条使用另一种配色方案(因为例如,1"和A"形成一对——它们代表在 2 个不同实验条件下的相同样本,这就是为什么我希望它们彼此相邻).

So I want to use one color scheme for the bars labelled with letters, another for those labelled with numbers (since e.g., "1" and "A" form a pair - they represent the same sample under 2 different experimental conditions, which is why I want them next to each other).

理想情况下,如果可以调整宽度以在一对条之间不留间隙(但对之间留有间隙),那就太好了.

Ideally, if width could be adapted to leave no gap between the bars of a pair (but a gap between pairs), that would be really nice.

但是现在,我不知道如何解决这个问题,所以任何建议都很好!

But right now, I have no idea how to go about this, so any advice would be great!

(我可以分别使用两个集合"的数据,这比较容易吗?也许是两个间隙较大的图相互交叉?)

(I can use both "sets" of data individually, of that is easier? Maybe do two plots with wide gaps, intersected with each other?)

推荐答案

您可以通过切片更优雅地做到这一点.可能更好的解决方案是对数据进行去隔行扫描.

You can do this more elegantly with slicing. A possibly better solution would be to de-interlace your data.

IDs = ["1","A","2","B","3","C","4","D","5","E"]

N = len(IDs)
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)

set_count = 2
set_label = np.vstack([j*ones(len(IDs)//set_count) for j in range(set_count)]).T.ravel()

property1 = numpy.array([1,3,4,2,3,5,6,7,3,2])
property2 = numpy.array(range(10))
property3 = numpy.array(range(10,0,-1))

props = [property1,property2,property3]

ind = numpy.arange(N/set_count)
width = 0.9
b_width = 0.9/set_count

color_sets = [['red','green','blue'],['black','magenta','yellow']]

for j in range(set_count):
    tmp_accum = np.zeros(len(props[0]))
    for k in range(len(props)):
        ax1.bar(ind +j*b_width, props[k][set_label==j], width=b_width, color=color_sets[j][k], bottom=tmp_accum[set_label==j])
        tmp_accum += props[k]


lab_ind = []
for j in range(set_count):

    lab_ind.append(ind + (j+.5)*b_width)

plt.xticks(np.vstack(lab_ind).T.ravel(), IDs )

它需要一些健全性检查,但它可以工作并且应该可以轻松扩展到 2 个以上的类和 3 个以上的属性(您只需要确保所有长度同步,使用 itertools.cycle颜色可能会有所帮助).

It needs some sanity checks, but it works and should scale to more than 2 classes and more than 3 properties easily (you just need to make sure that all of your lengths sync up, using itertools.cycle for the colors might help).

这篇关于堆叠的matplotlib栏中的替代颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆