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

查看:53
本文介绍了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天全站免登陆