栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何给熊猫/ matplotlib条形图自定义颜色

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何给熊猫/ matplotlib条形图自定义颜色

您可以将

color
选项指定为直接指向该
plot
功能的列表。

from matplotlib import pyplot as pltfrom itertools import cycle, isliceimport pandas, numpy as np  # I find np.random.randint to be better# Make the datax = [{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调用它们。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625669.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号