[英]Matplotlib: plotting multiple histograms side by side with different colors and different alphas
I want to plot multiple data into a histogram, where each dataset has it's own color and alpha value.我想将 plot 多个数据转换成直方图,其中每个数据集都有自己的颜色和 alpha 值。 I tried this:我试过这个:
colors = ['red', 'green', 'blue']
alphas = [1.0, 0.7, 0.3]
plt.hist([data1, data2, data3], bins = 15, label=['Data 1', 'Data 2', 'Data 3'], color = colors, alpha = alphas)
However, I get the following error:但是,我收到以下错误:
alpha must be numeric or None, not <class 'list'>
but I want different alpha values for each histogram.但我希望每个直方图都有不同的 alpha 值。 How can I achieve that?我怎样才能做到这一点?
I tried using zip
as @BigBen suggested in the comments:我尝试按照@BigBen 在评论中的建议使用zip
:
datas = [data1, data2, data3]
colors = ['red', 'green', 'blue']
alphas = [1.0, 0.7, 0.3]
labels = ['Data 1', 'Data 2', 'Data 3']
plt.savefig('images/bla.png')
for data, label, color, alpha in zip(datas, labels, colors, alphas):
plt.hist(data, bins=10, label=label, color=color, alpha=alpha)
However, the result looks like this:然而,结果看起来是这样的:
But I need something like this, but with different alpha values for different colors:但我需要这样的东西,但不同的 colors 有不同的 alpha 值:
matplotlib.colors.to_rgba is the solution. matplotlib.colors.to_rgba是解决方案。 With it, one can add alpha values to colors.使用它,可以将 alpha 值添加到 colors。
red = matplotlib.colors.to_rgba('red', alpha=1)
green = matplotlib.colors.to_rgba('green', alpha=0.7)
blue = matplotlib.colors.to_rgba('blue', alpha=0.3)
colors = [red, green, blue]
plt.hist([data1, data2, data3], bins = 15, label=['Data 1', 'Data 2', 'Data 3'], color = colors)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.