Opacity is misleading when plotting two histograms simultaneously with matplotlib

Say I have two histograms and I set the opacity using the hist parameter: 'alpha = 0.5'

I built two histograms, but I get three colors! I understand that this makes sense in terms of opacity.

But! It is very confusing to show someone a graph of two things with three colors. Can I somehow set the minimum strip for each bin in front without transparency?

Graph example

enter image description here

+6
source share
1 answer

The usual way to handle this problem is to have areas with little separation. This is done by default when multiple datasets are specified by plt.hist :

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

Example 1

Instead, you can stack them (this can be done above using the histtype='barstacked' argument), but note that the order is incorrect.

This can be fixed by individually checking each pair of dots to see which one is larger, and then using zorder to determine which one will be the first. For simplicity, I use the output of the above code (for example, n is two arrays with the sum of the number of points in each box for x and y):

 n_x = n[0] n_y = n[1] for i 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 is the resulting image: enter image description here

Changing the order in this way, the image looks very strange, which is probably why it is not implemented and requires hacking. I would stick with a little sorting method, anyone who is used to these graphs assumes they take the same x value.

+8
source

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


All Articles