如何在matplotlib中选择直方图条的独特颜色? [英] How to pick unique colors of histogram bars in matplotlib?

查看:268
本文介绍了如何在matplotlib中选择直方图条的独特颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在同一块图上绘制几个直方图,但我发现有些颜色分配给不同的系列,这使我有些不安.有没有一种方法可以使颜色条成为唯一的?

I am trying to plot a several histogram on the same plot but I figured out that some colors are assigned to different series, which bother me a little. Is there a way of forcing color bars to be unique ?

这适用于较小的数据集,但是当我使用大量数据时,我看到这个问题又回来了

That works for small data set, but when I use a lot of data, I see this problem coming back

这是一个示例,蓝色两次分配给两个不同的数据样本

here is an example, the blue color is assigned twice to two different data samples

所有示例和将颜色归因于matplotlib中的直方图的解决方案(至少我发现的那些)建议将x轴归一化,如

All the examples and the solutions to attribute colors to histograms in matplotlib (at least those I found) are suggesting to normalize x axis between 0 and 1 like this example , but this is not what I want to have because it is very important to have the real values in my case.

还有其他解决方案吗?

谢谢

编辑

我随附的一种解决方案是将cmap调色板转换为numpy数组,并通过调用此调色板使用pyplot历史颜色

One solution I came with is to convert a cmap palette to a numpy array and use pyplot hist color by calling this palette

N = len(list_of_samples)
sample_colors = cm.get_cmap('RdYlBu', N)
palette = sample_colors(np.arange(N))

但这仅适用于历史记录的绘图功能,我得到了此错误消息

But this works only for hist for plot function I got this error message

ValueError: to_rgba: Invalid rgba arg "[[ 0.64705884  0.          0.14901961  1.        ]
 [ 0.89187675  0.2907563   0.20000001  1.        ]
 [ 0.98711484  0.64593837  0.36358543  1.        ]
 [ 0.99719888  0.91316527  0.61736696  1.        ]
 [ 0.91316529  0.96638656  0.90868344  1.        ]
 [ 0.63977591  0.82633053  0.90028011  1.        ]
 [ 0.34957983  0.55294117  0.75462185  1.        ]
 [ 0.19215687  0.21176471  0.58431375  1.        ]]"
only length-1 arrays can be converted to Python scalars

推荐答案

直方图的解决方案如下:

A solution for histograms is as follows:

import pylab as pl

N, bins, patches = pl.hist(pl.rand(1000), 20)

jet = pl.get_cmap('jet', len(patches))

for i in range(len(patches)):
    patches[i].set_facecolor(jet(i))

结果:

我希望这就是您想要的.

I hope that's what you are looking for.

这篇关于如何在matplotlib中选择直方图条的独特颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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