Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Let's say I have two histograms and I set the opacity using the parameter of hist: 'alpha=0.5'

I have plotted two histograms yet I get three colors! I understand this makes sense from an opacity point of view.

But! It makes is very confusing to show someone a graph of two things with three colors. Can I just somehow set the smallest bar for each bin to be in front with no opacity?

Example graph

enter image description here

like image 372
mike Avatar asked Aug 26 '13 21:08

mike


1 Answers

The usual way this issue is handled is to have the plots with some small separation. This is done by default when plt.hist is given multiple sets of data:

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

You instead which to stack them (this could be done above using the argument histtype='barstacked') but notice that the ordering is incorrect.

This can be fixed by individually checking each pair of points to see which is larger and then using zorder to set which one comes first. For simplicity I am using the output of the code above (e.g n is two stacked arrays of the number of points in each bin 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

By changing the ordering like this the image looks very weird indeed, this is probably why it is not implemented and needs a hack to do it. I would stick with the small separation method, anyone used to these plots assumes they take the same x-value.

like image 188
Greg Avatar answered Oct 21 '22 03:10

Greg