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()更多推荐
发布评论