8

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

sodd
  • 12,482
  • 3
  • 54
  • 62
mike
  • 143
  • 3
  • 8
  • 2
    If you don't actually want to have `alpha<1` than just figure out what color the translucent color on top of white is and use opaque bars that color. – tacaswell Aug 26 '13 at 22:01
  • 2
    You should include the code you have used to produce the plot, as less people are likely to write the entire code for you, rather than fixing your existing code. – sodd Aug 27 '13 at 10:02
  • What's the code you used to generate this graph? – tommy.carstensen Dec 08 '14 at 09:30

1 Answers1

9

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.

Community
  • 1
  • 1
Greg
  • 11,654
  • 3
  • 44
  • 50