gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 20:46:17 30 4
gpt4 key购买 nike

我无法控制使用 Matplotlib 绘制的直方图的颜色和线条样式 hist功能与 stacked=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, 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 版。

最佳答案

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

关于python-2.7 - 用于 Matplotlib 堆叠直方图的 facecolor kwarg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17512730/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com