简体   繁体   English

使用matplotlib.pyplot.imshow()时如何确定颜色?

[英]How to determine the colours when using matplotlib.pyplot.imshow()?

I'm using imshow() to draw a 2D numpy array, so for example: 我正在使用imshow()来绘制2D numpy数组,例如:

my_array = [[ 2.  0.  5.  2.  5.]
            [ 3.  2.  0.  1.  4.]
            [ 5.  0.  5.  4.  4.]
            [ 0.  5.  2.  3.  4.]
            [ 0.  0.  3.  5.  2.]]

plt.imshow(my_array, interpolation='none', vmin=0, vmax=5)

which plots this image: 绘制此图像:

在此输入图像描述

What I want to do however, is change the colours, so that for example 0 is RED, 1 is GREEN, 2 is ORANGE, you get what I mean. 然而,我想要做的是改变颜色,例如0是RED,1是GREEN,2是ORANGE,你明白我的意思。 Is there a way to do this, and if so, how? 有没有办法做到这一点,如果是这样,怎么样?

I've tried doing this by changing the entries in the colourmap, like so: 我试过通过更改colourmap中的条目来尝试这样做,如下所示:

    cmap = plt.cm.jet
    cmaplist = [cmap(i) for i in range(cmap.N)]
    cmaplist[0] = (1,1,1,1.0)
    cmaplist[1] = (.1,.1,.1,1.0)
    cmaplist[2] = (.2,.2,.2,1.0)
    cmaplist[3] = (.3,.3,.3,1.0)
    cmaplist[4] = (.4,.4,.4,1.0)
    cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)

but it did not work as I expected, because 0 = the first entry in the colour map, but 1 for example != the second entry in the colour map, and so only 0 is drawn diffrently: 但它没有像我预期的那样工作,因为0 =颜色映射中的第一个条目,但是例如1个!=颜色映射中的第二个条目,因此只有0被不同地绘制:

在此输入图像描述

I think the easiest way is to use a ListedColormap , and optionally with a BoundaryNorm to define the bins . 我认为最简单的方法是使用ListedColormap ,并可选择使用BoundaryNorm来定义bins Given your array above: 鉴于上面的数组:

import matplotlib.pyplot as plt
import matplotlib as mpl

colors = ['red', 'green', 'orange', 'blue', 'yellow', 'purple']
bounds = [0,1,2,3,4,5,6]

cmap = mpl.colors.ListedColormap(colors)
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)

plt.imshow(my_array, interpolation='none', cmap=cmap, norm=norm)

Because your data values map 1-on-1 with the boundaries of the colors, the normalizer is redundant. 由于数据值与颜色边界一对一地映射,因此normalizer是多余的。 But i have included it to show how it can be used. 但我已经把它包括在内以表明它是如何使用的。 For example when you want the values 0,1,2 to be red, 3,4,5 green etc, you would define the boundaries as [0,3,6...]. 例如,当您希望值0,1,2为红色,3,4,5绿色等时,您可以将边界定义为[0,3,6 ...]。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在matplotlib.pyplot.imshow中使用“范围” - how to use 'extent' in matplotlib.pyplot.imshow matplotlib.pyplot.imshow 中的神器 - Artifact in matplotlib.pyplot.imshow 使用Jupyter重新绘制matplotlib.pyplot.imshow - Redraw matplotlib.pyplot.imshow in place using Jupyter 在子图环境中使用时,matplotlib.pyplot.imshow的边距 - Margins of matplotlib.pyplot.imshow when used in subplot environment matplotlib.pyplot.imshow:使用属性“sharex”和“sharey”时删除绘图中的空白区域 - matplotlib.pyplot.imshow: removing white space within plots when using attributes “sharex” and “sharey” 使用矩阵第一行的值作为matplotlib.pyplot.imshow的刻度 - Using the values of the first row of a matrix as ticks for matplotlib.pyplot.imshow 图像在matplotlib.pyplot.imshow上看起来不错,但是当在QT GUi上使用Qimage显示时,图像却严重扭曲 - Image looks good on matplotlib.pyplot.imshow , but is horribly distorted when shown on QT GUi using Qimage 如何在matplotlib.pyplot.imshow中标记单元格(绘制单元格边框) - How to mark cells in matplotlib.pyplot.imshow (drawing cell borders) 当没有足够的分辨率显示所有数据时,matplotlib.pyplot.imshow如何“压缩”数据? - How does matplotlib.pyplot.imshow 'squeeze' the data when there is not enough resolution to show all data? python matplotlib.pyplot.imshow tkinter错误 - python matplotlib.pyplot.imshow tkinter error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM