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

点点圈 提交于 2019-12-21 04:40:17

问题


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


回答1:


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])

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:

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.



来源:https://stackoverflow.com/questions/18453570/opacity-misleading-when-plotting-two-histograms-at-the-same-time-with-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!