Matplotlib堆叠直方图的facecolor kwarg [英] facecolor kwarg for Matplotlib stacked histogram

查看:139
本文介绍了Matplotlib堆叠直方图的facecolor kwarg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法控制使用Matplotlib的hist函数和stacked=True绘制的直方图的颜色和线条样式.对于单个非堆叠直方图,我没有问题:

I am having trouble controlling the color and linestyle of histogram plotted using Matplotlib's hist function with stacked=True. For a single non-stacked histogram, I have no trouble:

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

但是,当我引入其他直方图时,

However, when I introduce additional histograms,

import pylab as P

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

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

它会引发以下错误:

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

使用color=['lightblue', 'lightgreen', 'crimson']选项确实可以,但是我想分别控制填充和线条颜色,同时能够使用命名的Matplotlib颜色.我正在使用Matplotlib的1.2.1版本.

Using the color=['lightblue', 'lightgreen', 'crimson'] option does work, but I would like to have direct control of the fill and line colors separately while being able to use the named Matplotlib colors. I am using version 1.2.1 of Matplotlib.

推荐答案

facecolor必须是单个命名的颜色,而不是列表,但要添加此颜色 使用P.hist后可能会为您完成工作:

facecolor needs to be a single named color, not a list, but adding this after your P.hist usage might get the job done for you:

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

这篇关于Matplotlib堆叠直方图的facecolor kwarg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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