加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python-2.7 – 用于Matplotlib叠加直方图的facecolor kwarg

发布时间:2020-12-16 21:36:39 所属栏目:Python 来源:网络整理
导读:我无法控制使用Matplotlib的hist函数绘制的直方图的颜色和线型,堆叠= True.对于单个非堆叠直方图,我没有遇到麻烦: import pylab as Pmu,sigma = 200,25x0 = mu + sigma*P.randn(10000)n,bins,patches = P.hist( x0,20,histtype='stepfilled',facecolor='lig
我无法控制使用Matplotlib的hist函数绘制的直方图的颜色和线型,堆叠= True.对于单个非堆叠直方图,我没有遇到麻烦:
import pylab as P

mu,sigma = 200,25
x0 = mu + sigma*P.randn(10000)

n,bins,patches = P.hist(
    x0,20,histtype='stepfilled',facecolor='lightblue'
    )

但是,当我引入额外的直方图时,

import pylab as P

mu,25
x0 = mu + sigma*P.randn(10000)
x1 = mu + sigma*P.randn(7000)
x2 = mu + sigma*P.randn(3000)

n,patches = P.hist(
    [x0,x1,x2],stacked=True,facecolor=['lightblue','lightgreen','crimson']
    )

它会引发以下错误:

ValueError: to_rgba: Invalid rgba arg "['lightblue','crimson']"
could not convert string to float: lightblue

使用color = [‘lightblue’,’lightgreen’,’crimson’]选项确实有效,但我希望能够直接控制填充和线条颜色,同时能够使用命名的Matplotlib颜色.我使用的是Matplotlib 1.2.1版.

解决方法

facecolor需要是一个命名颜色,而不是列表,但添加它
使用P.hist后可能会为您完成工作:
for patch in patches[0]: patch.set_facecolor('lightblue')
for patch in patches[1]: patch.set_facecolor('lightgreen')
for patch in patches[2]: patch.set_facecolor('crimson')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读