matplotlib color map - predefine mappings to values?

让人想犯罪 __ 提交于 2019-12-18 05:18:08

问题


I have an array that I am viewing using imshow(). (imsave() really, but the process should be identical).

I know that the values in the array will be between 0-9 and wonder if it is possible to use cmap to set each output to a specific 'color'. Perhaps by mapping these to a dict?


回答1:


Just use a ListedColormap.

As a quick (but ugly) example:

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

cmap = ListedColormap(['red', 'green', 'blue', 'black'], 'indexed')

fig, ax = plt.subplots()
im = ax.imshow([range(4)], interpolation='none', cmap=cmap)
fig.colorbar(im)
plt.show()



来源:https://stackoverflow.com/questions/15207052/matplotlib-color-map-predefine-mappings-to-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!