matplotlib 中矩阵的自定义着色
Custom coloration for matrix in matplotlib
我想绘制三个矩阵,但我想出的唯一解决方案是一个接一个地绘制,这样就只剩下最后一个绘制的矩阵了。
ax.imshow(mat1, cmap='Blues', interpolation='nearest')
ax.imshow(mat2, cmap='binary', interpolation='nearest')
ax.imshow(mat3, cmap='autumn', interpolation='nearest') # actual plot
我想要的是将三个矩阵中的所有 0 显示为白色,并根据矩阵以不同的色调显示更高的值,例如:蓝色、黑色和红色。此外,在该示例中,红色单元格优先于黑色单元格,黑色单元格优先于蓝色单元格。我想象的解决方案是一个函数,给定一个三元组(蓝色,黑色,红色),每个组件具有不同的值,returns 单元格应该着色的颜色,并将其提供给 ColorMap , 但我真的不知道该怎么做,或者是否可能。
欢迎并感谢各种帮助甚至不同的解决方案(这是最有可能发生的)。提前致谢。
你想要一个第四图像,每个点的RGB值是相应点前三个矩阵的单个值的函数?如果是这样,你能产生从三个值到 RGB 第四个值的代数吗?
您的问题表明您对绘图如何将数据转换为颜色感到困惑。颜色图采用 single-valued 数据,对其进行标准化,并将其映射到某个命名的颜色数组中。 0 值可能会映射到任何颜色,具体取决于颜色图和其余数据。
位图定义每个像素的(红色、绿色、蓝色)值。正确的位图有 header 部分,但数据是 (m,n,3) 数组。 imshow
仅绘制该数组;它期望 RGB 值在 [0,1] 范围内。
如果您有三个数据矩阵,则必须选择如何将这些值映射到 RGB 值。这是一个包含三种映射到 RGB 的示例。前两行是具有一系列值的虚拟数据,以颜色图或最简单的 RGB 表示形式显示。最后一行显示了使用整个色彩空间将所有三个虚拟矩阵组合成一个图像的方法。
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
#dummy data
x = 8
y = 15
mat = []
mat.append(np.arange(x * y).reshape((x, y)) / float(x * y) )
mat.append(np.arange(x * y).reshape((y, x)).T / float(x* y))
mat.append(np.arange(y) * np.arange(x)[:,np.newaxis] / float(99))
# NOTE: this data is approximately in the RGB range. If yours isn't, normalize,
# here or in your makeRGB function.
# (The colormap normalizes single-valued data).
fig, axs = plt.subplots(figsize=(7,4), nrows=3, ncols=3,
gridspec_kw={'hspace':0.6})
axs[0,0].imshow(mat[0], cmap='Reds', interpolation='nearest')
axs[0,1].imshow(mat[1], cmap='Greens', interpolation='nearest')
axs[0,2].imshow(mat[2], cmap='Blues', interpolation='nearest')
axs[0,0].set_xlabel('Reds Colormap')
axs[0,1].set_xlabel('Greens Colormap')
axs[0,2].set_xlabel('Blues Colormap')
def asOneHue(mat, hue):
"""
Use a single-valued matrix to represent one hue in a RGB file.'
"""
RGBout = np.zeros((len(mat),len(mat[0]),3))
RGBout[:,:,i] = mat
return RGBout
for i in (0,1,2):
axs[1,i].imshow(asOneHue(mat[i],i))
axs[1,0].set_xlabel('Reds bitmap')
axs[1,1].set_xlabel('Greens bitmap')
axs[1,2].set_xlabel('Blues bitmap')
# different ways to combine 3 values
def makeRGB0(mats):
RGBout = np.zeros((len(mats[0]),len(mats[0][0]),3))
#RGBout = np.ones((len(mats[0]),len(mats[0][0]),3))
for i in (0,1,2):
RGBout[:,:,i] = mats[i]
return RGBout
axs[2,0].imshow(makeRGB0(mat))
axs[2,0].set_xlabel('Color layers')
def makeRGB1(mats):
RGBout = np.zeros((len(mats[0]),len(mats[0][0]),3))
i,j,k = RGBout.shape
for x in range(i):
for y in range(j):
RGBout[x,y] = (mats[0][x][y] / 2,
mats[1][x][y],
1 - mats[2][x][y])
return RGBout
axs[2,1].imshow(makeRGB1(mat))
axs[2,1].set_xlabel('Algebraic')
def makeRGB2(mats):
RGBout = np.zeros((len(mats[0]),len(mats[0][0]),3))
i,j,k = RGBout.shape
for x in range(i):
for y in range(j):
if mats[0][x][y] > .8:
RGBout[x,y] = (mats[0][x][y],
0,
0)
elif mats[1][x][y] > .8:
RGBout[x,y] = (0,
mats[1][x][y],
0)
else:
RGBout[x,y] = (mats[0][x][y],
mats[1][x][y],
mats[2][x][y])
return RGBout
axs[2,2].imshow(makeRGB2(mat))
axs[2,2].set_xlabel('If-else')
plt.show()
我想绘制三个矩阵,但我想出的唯一解决方案是一个接一个地绘制,这样就只剩下最后一个绘制的矩阵了。
ax.imshow(mat1, cmap='Blues', interpolation='nearest')
ax.imshow(mat2, cmap='binary', interpolation='nearest')
ax.imshow(mat3, cmap='autumn', interpolation='nearest') # actual plot
我想要的是将三个矩阵中的所有 0 显示为白色,并根据矩阵以不同的色调显示更高的值,例如:蓝色、黑色和红色。此外,在该示例中,红色单元格优先于黑色单元格,黑色单元格优先于蓝色单元格。我想象的解决方案是一个函数,给定一个三元组(蓝色,黑色,红色),每个组件具有不同的值,returns 单元格应该着色的颜色,并将其提供给 ColorMap , 但我真的不知道该怎么做,或者是否可能。
欢迎并感谢各种帮助甚至不同的解决方案(这是最有可能发生的)。提前致谢。
你想要一个第四图像,每个点的RGB值是相应点前三个矩阵的单个值的函数?如果是这样,你能产生从三个值到 RGB 第四个值的代数吗?
您的问题表明您对绘图如何将数据转换为颜色感到困惑。颜色图采用 single-valued 数据,对其进行标准化,并将其映射到某个命名的颜色数组中。 0 值可能会映射到任何颜色,具体取决于颜色图和其余数据。
位图定义每个像素的(红色、绿色、蓝色)值。正确的位图有 header 部分,但数据是 (m,n,3) 数组。 imshow
仅绘制该数组;它期望 RGB 值在 [0,1] 范围内。
如果您有三个数据矩阵,则必须选择如何将这些值映射到 RGB 值。这是一个包含三种映射到 RGB 的示例。前两行是具有一系列值的虚拟数据,以颜色图或最简单的 RGB 表示形式显示。最后一行显示了使用整个色彩空间将所有三个虚拟矩阵组合成一个图像的方法。
# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np
#dummy data
x = 8
y = 15
mat = []
mat.append(np.arange(x * y).reshape((x, y)) / float(x * y) )
mat.append(np.arange(x * y).reshape((y, x)).T / float(x* y))
mat.append(np.arange(y) * np.arange(x)[:,np.newaxis] / float(99))
# NOTE: this data is approximately in the RGB range. If yours isn't, normalize,
# here or in your makeRGB function.
# (The colormap normalizes single-valued data).
fig, axs = plt.subplots(figsize=(7,4), nrows=3, ncols=3,
gridspec_kw={'hspace':0.6})
axs[0,0].imshow(mat[0], cmap='Reds', interpolation='nearest')
axs[0,1].imshow(mat[1], cmap='Greens', interpolation='nearest')
axs[0,2].imshow(mat[2], cmap='Blues', interpolation='nearest')
axs[0,0].set_xlabel('Reds Colormap')
axs[0,1].set_xlabel('Greens Colormap')
axs[0,2].set_xlabel('Blues Colormap')
def asOneHue(mat, hue):
"""
Use a single-valued matrix to represent one hue in a RGB file.'
"""
RGBout = np.zeros((len(mat),len(mat[0]),3))
RGBout[:,:,i] = mat
return RGBout
for i in (0,1,2):
axs[1,i].imshow(asOneHue(mat[i],i))
axs[1,0].set_xlabel('Reds bitmap')
axs[1,1].set_xlabel('Greens bitmap')
axs[1,2].set_xlabel('Blues bitmap')
# different ways to combine 3 values
def makeRGB0(mats):
RGBout = np.zeros((len(mats[0]),len(mats[0][0]),3))
#RGBout = np.ones((len(mats[0]),len(mats[0][0]),3))
for i in (0,1,2):
RGBout[:,:,i] = mats[i]
return RGBout
axs[2,0].imshow(makeRGB0(mat))
axs[2,0].set_xlabel('Color layers')
def makeRGB1(mats):
RGBout = np.zeros((len(mats[0]),len(mats[0][0]),3))
i,j,k = RGBout.shape
for x in range(i):
for y in range(j):
RGBout[x,y] = (mats[0][x][y] / 2,
mats[1][x][y],
1 - mats[2][x][y])
return RGBout
axs[2,1].imshow(makeRGB1(mat))
axs[2,1].set_xlabel('Algebraic')
def makeRGB2(mats):
RGBout = np.zeros((len(mats[0]),len(mats[0][0]),3))
i,j,k = RGBout.shape
for x in range(i):
for y in range(j):
if mats[0][x][y] > .8:
RGBout[x,y] = (mats[0][x][y],
0,
0)
elif mats[1][x][y] > .8:
RGBout[x,y] = (0,
mats[1][x][y],
0)
else:
RGBout[x,y] = (mats[0][x][y],
mats[1][x][y],
mats[2][x][y])
return RGBout
axs[2,2].imshow(makeRGB2(mat))
axs[2,2].set_xlabel('If-else')
plt.show()