Pylab

编程入门 行业动态 更新时间:2024-10-25 19:33:39
Pylab - 根据RGB轴更改历史图颜色(Pylab - Change historgram colors based on RGB axis)

我有一个python字典我用来获得直方图:

colors = {(97, 103, 105): 638059, (129, 140, 143): 562526, (55, 58, 62): 431610, (189, 193, 193): 460605} import pylab as pl import numpy as np dict_index = np.arange(len(colors)) pl.bar(dict_index, colors.values(), align='center', width=0.5) pl.xticks(dict_index, colors.keys()) ymax = max(colors.values()) + 1 pl.ylim(0, ymax) pl.show()

键代表RGB颜色代码,我的值是像素数。

我正在尝试获得直方图(或树形图,但这将是额外的),其中每个条形采用它代表的RGB颜色。

我该怎么办?

谢谢,

I have a python dictionary I'm using to get a histogram:

colors = {(97, 103, 105): 638059, (129, 140, 143): 562526, (55, 58, 62): 431610, (189, 193, 193): 460605} import pylab as pl import numpy as np dict_index = np.arange(len(colors)) pl.bar(dict_index, colors.values(), align='center', width=0.5) pl.xticks(dict_index, colors.keys()) ymax = max(colors.values()) + 1 pl.ylim(0, ymax) pl.show()

The keys represent RGB color codes and my values are number of pixels.

I'm trying to get a histogram (or a treemap but that would be extra) where each bar takes on the RGB color it represents.

How would I do that?

Thank you,

最满意答案

尝试pl.bar中的颜色选项。 颜色需要标准化为0-1。

colors = {(97, 103, 105): 638059, (129, 140, 143): 562526, (55, 58, 62): 431610, (189, 193, 193): 460605} normalizedcolors = [[i/256 for i in j] for j in colors.keys()] import pylab as pl import numpy as np dict_index = np.arange(len(colors)) pl.bar(dict_index, colors.values(), align='center', width=0.5, color = normalizedcolors) pl.xticks(dict_index, colors.keys()) ymax = max(colors.values()) + 1 pl.ylim(0, ymax) pl.show()

Try the color option in pl.bar. Colors need to be normalized to 0-1.

colors = {(97, 103, 105): 638059, (129, 140, 143): 562526, (55, 58, 62): 431610, (189, 193, 193): 460605} normalizedcolors = [[i/256 for i in j] for j in colors.keys()] import pylab as pl import numpy as np dict_index = np.arange(len(colors)) pl.bar(dict_index, colors.values(), align='center', width=0.5, color = normalizedcolors) pl.xticks(dict_index, colors.keys()) ymax = max(colors.values()) + 1 pl.ylim(0, ymax) pl.show()

更多推荐

本文发布于:2023-08-04 18:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1420705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Pylab

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!