Python Matplotlib pcolormesh,单独的数据颜色和颜色亮度信息
我想用matplotlib在网格上绘制数据,目前正在尝试pcolormesh。 数据组织在两个numpy数组中,即数据本身和一个colorInformation数组 下面的代码绘制了数据数组(1是红色的,0是蓝色的),但是我也有colorInformation数组,它应该根据每个对应单元格正方形的值更改其亮度,同时保持颜色不变 例如,数据中的行[1,0,0,1]应具有应用于绘图的亮度值[0.1,0.12,0.02,0.01],以便该行可视化为[红色和亮度0.1,蓝色和亮度0.12,蓝色和亮度0.02,红色和亮度0.01] 如何做到这一点Python Matplotlib pcolormesh,单独的数据颜色和颜色亮度信息,python,numpy,matplotlib,plot,Python,Numpy,Matplotlib,Plot,我想用matplotlib在网格上绘制数据,目前正在尝试pcolormesh。 数据组织在两个numpy数组中,即数据本身和一个colorInformation数组 下面的代码绘制了数据数组(1是红色的,0是蓝色的),但是我也有colorInformation数组,它应该根据每个对应单元格正方形的值更改其亮度,同时保持颜色不变 例如,数据中的行[1,0,0,1]应具有应用于绘图的亮度值[0.1,0.12,0.02,0.01],以便该行可视化为[红色和亮度0.1,蓝色和亮度0.12,蓝色和亮度0.
import numpy as np
import matplotlib.pyplot as plt
data = np.array([[1, 0, 0, 1],
[0, 0, 1, 1],
[0, 0, 0, 1]])
colorInformation = np.array([[0.1, 0.12, 0.02, 0.01],
[0.12, 0.15, 0.18, 0.2],
[0.3, 0.34, 0.41, 0.32]])
fig, ax = plt.subplots()
heatmap = ax.pcolormesh(data)
plt.show()
我建议您自己定制彩色地图来解决这个问题
from matplotlib.colors import LinearSegmentedColormap
data = np.array([[1, 0, 0, 1],
[0, 0, 1, 1],
[0, 0, 0, 1]])
colorInformation = np.array([[0.1, 0.12, 0.02, 0.01],
[0.12, 0.15, 0.18, 0.2],
[0.3, 0.34, 0.41, 0.32]])
alpha_up=abs(((data*2-1)*colorInformation).max())
alpha_low=abs(((data*2-1)*colorInformation).min())
mid=alpha_low/(alpha_up+alpha_low)
cdict1 = {'red': ((0.0, 1.0, 1.0),
(mid, 1.0, 0.0),
(1.0, 0.0, 0.0)),
'green': ((0.0, 0.0, 0.0),
(1.0, 0.0, 0.0)),
'blue': ((0.0, 0.0, 0.0),
(mid, 0.0, 1.0),
(1.0, 1.0, 1.0)),
'alpha': ((0.0, alpha_low, alpha_low),
(mid, 0.0, 0.0),
(1.0, alpha_up, alpha_up))
}
red_blue = LinearSegmentedColormap('red_blue', cdict1)
fig, ax = plt.subplots()
heatmap = ax.pcolormesh((data*2-1)*colorInformation, cmap=red_blue)
或者,您可以只更改红色和蓝色,而不使用alpha通道