How to choose unique histogram colors in matplotlib?

I am trying to build several histograms on the same plot, but I realized that some colors are assigned to different series, which bothers me a bit. Is there a way to make color bars be unique?

This works for a small data set, but when I use a lot of data, I see that this problem returns

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

enter image description here

All examples and solutions of color attributes for histograms in matplotlib (at least the ones I found) suggest normalizing the x axis between 0 and 1, like this example , but this is not what I want, because it is very important to have real values in my case.

Is there any other solution?

thanks

EDIT

One solution I came up with is to convert the cmap palette to a numpy array and use the color of the pyplot histogram to invoke this palette

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

But this only works for the hist function for plotting. 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 
+6
source share
1 answer

The solution for the 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)) 

Result:

enter image description here

I hope you are looking.

+6
source

Source: https://habr.com/ru/post/974065/


All Articles