Matplotlib:使用非透明边缘绘制透明直方图 [英] Matplotlib: plotting transparent histogram with non transparent edge

查看:619
本文介绍了Matplotlib:使用非透明边缘绘制透明直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制直方图,我想将三个数据集一起绘制,每个数据集具有不同的颜色和线型(虚线,点线等)。
我也提供一些透明度,以查看重叠的条形。

I am plotting a histogram, and I have three datasets which I want to plot together, each one with different colours and linetype (dashed, dotted, etc). I am also giving some transparency, in order to see the overlapping bars.

我要说的是,我希望每个条形的边缘都不会变得透明就像内部一样。
这是一个示例:

The point is that I would like the edge of each bar not to become transparent as the inner part does. Here is an example:

import matplotlib.pyplot as plt
import numpy as np

x = np.random.random(20)
y =np.random.random(20)
z= np.random.random(20)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(x, bins=np.arange(0, 1, 0.1), ls='dashed', alpha = 0.5, lw=3, color= 'b')
ax.hist(y, bins=np.arange(0, 1, 0.1), ls='dotted', alpha = 0.5, lw=3, color= 'r')
ax.hist(z, bins=np.arange(0, 1, 0.1), alpha = 0.5, lw=3, color= 'k')
ax.set_xlim(-0.5, 1.5)
ax.set_ylim(0, 7)
plt.show()

推荐答案

plt.hist 接受其他关键字参数,这些参数传递给 matplotlib.patches.Patch 。特别是,您可以传递 fc = 参数,该参数可让您使用(R,G,B,A)创建直方图时的元组。更改facecolor的alpha值不会影响边缘的透明度:

plt.hist accepts additional keyword arguments that are passed to the constructor for matplotlib.patches.Patch. In particular you can pass an fc= argument which lets you set the patch facecolor using an (R, G, B, A) tuple when you create the histograms. Changing the alpha value of the facecolor does not affect the transparency of the edges:

ax.hist(x, bins=np.arange(0, 1, 0.1), ls='dashed', lw=3, fc=(0, 0, 1, 0.5))
ax.hist(y, bins=np.arange(0, 1, 0.1), ls='dotted', lw=3, fc=(1, 0, 0, 0.5))
ax.hist(z, bins=np.arange(0, 1, 0.1), lw=3, fc=(0, 0, 0, 0.5))

这篇关于Matplotlib:使用非透明边缘绘制透明直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆