python 条形图指定颜色_python – 如何给一个熊猫/ matplotlib条形图自定义颜色

您可以将颜色选项直接指定为绘图函数的列表。

from matplotlib import pyplot as plt

from itertools import cycle, islice

import pandas, numpy as np # I find np.random.randint to be better

# Make the data

x = [{i:np.random.randint(1,5)} for i in range(10)]

df = pandas.DataFrame(x)

# Make a list by cycling through the colors you care about

# to match the length of your data.

my_colors = list(islice(cycle(['b', 'r', 'g', 'y', 'k']), None, len(df)))

# Specify this list of colors as the `color` option to `plot`.

df.plot(kind='bar', stacked=True, color=my_colors)

要定义自己的自定义列表,您可以执行以下几个操作,或者只是查找Matplotlib技术,通过其RGB值定义颜色项目等。您可以随意使用这个复杂。

my_colors = ['g', 'b']*5 # <-- this concatenates the list to itself 5 times.

my_colors = [(0.5,0.4,0.5), (0.75, 0.75, 0.25)]*5 # <-- make two custom RGBs and repeat/alternate them over all the bar elements.

my_colors = [(x/10.0, x/20.0, 0.75) for x in range(len(df))] # <-- Quick gradient example along the Red/Green dimensions.

最后一个例子产生了以下简单的颜色梯度:

我没有玩足够长的时间,以解决如何强迫传说来拾取定义的颜色,但我相信你可以做到。

一般来说,一个大的建议是直接使用Matplotlib的函数。从Pandas调用他们是确定,但我发现你得到更好的选择和性能,直接从Matplotlib。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值