python - Opacity misleading when plotting two histograms at the same time with matplotlib -


let's have 2 histograms , set opacity using parameter of hist: 'alpha=0.5'

i have plotted 2 histograms yet 3 colors! understand makes sense opacity point of view.

but! makes confusing show graph of 2 things 3 colors. can somehow set smallest bar each bin in front no opacity?

example graph

enter image description here

the usual way issue handled have plots small separation. done default when plt.hist given multiple sets of data:

import pylab plt  x = 200 + 25*plt.randn(1000) y = 150 + 25*plt.randn(1000) n, bins, patches = plt.hist([x, y]) 

example 1

you instead stack them (this done above using argument histtype='barstacked') notice ordering incorrect.

this can fixed individually checking each pair of points see larger , using zorder set 1 comes first. simplicity using output of code above (e.g n 2 stacked arrays of number of points in each bin x , y):

n_x = n[0] n_y = n[1] in range(len(n[0])):     if n_x[i] > n_y[i]:         zorder=1     else:         zorder=0     plt.bar(bins[:-1][i], n_x[i], width=10)     plt.bar(bins[:-1][i], n_y[i], width=10, color="g", zorder=zorder) 

here resulting image: enter image description here

by changing ordering image looks weird indeed, why not implemented , needs hack it. stick small separation method, used these plots assumes take same x-value.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

c++ - Linked List error when inserting for the last time -

python - TypeError: can only concatenate tuple (not "float") to tuple -