我第一次使用matplotlib来可视化以下矩阵:
[[ 150. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[ 150. 69.388 35.36 18.211 7.851 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -0.03 -0.047 -0.044 -0.027 0. ]
[ 150. 92.192 53.842 29.633 13.192 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -0.075 -0.112 -0.104 -0.062 0. ]
[ 150. 95.538 58.184 33.287 15.285 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -0.156 -0.221 -0.199 -0.116 0. ]
[ 150. 81.776 50.068 30.045 14.659 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -0.331 -0.416 -0.357 -0.203 0. ]
[ 0. 31.498 30.267 22.168 13.308 5.653 2.766 1.5 0.86 0.492 0.254 0.082 -0.058 -0.182 -0.298 -0.41 -0.515 -0.617 -0.751 -0.754 -0.609 -0.34 0. ]
[ 0. 13.948 17.335 15.05 10.751 6.537 3.913 2.374 1.447 0.854 0.443 0.13 -0.132 -0.372 -0.602 -0.826 -1.034 -1.204 -1.301 -1.241 -0.985 -0.548 0. ]
[ 0. 6.958 10.074 9.949 8.107 5.832 3.973 2.635 1.7 1.034 0.534 0.129 -0.229 -0.57 -0.913 -1.258 -1.59 -1.864 -2.007 -1.924 -1.543 -0.866 0. ]
[ 0. 3.812 6.053 6.564 5.896 4.712 3.511 2.493 1.685 1.048 0.529 0.079 -0.342 -0.768 -1.22 -1.705 -2.203 -2.654 -2.941 -2.904 -2.395 -1.375 0. ]
[ 0. 2.237 3.761 4.358 4.202 3.609 2.867 2.142 1.498 0.943 0.455 0.002 -0.452 -0.94 -1.495 -2.139 -2.864 -3.607 -4.2 -4.357 -3.76 -2.237 0. ]
[ 0. 1.375 2.396 2.906 2.943 2.656 2.206 1.708 1.223 0.771 0.345 -0.076 -0.526 -1.045 -1.682 -2.491 -3.509 -4.71 -5.895 -6.563 -6.052 -3.812 0. ]
[ 0. 0.867 1.543 1.925 2.009 1.865 1.592 1.261 0.915 0.573 0.232 -0.126 -0.531 -1.031 -1.698 -2.633 -3.971 -5.831 -8.105 -9.948 -10.073 -6.958 0. ]
[ 0. 0.548 0.986 1.242 1.302 1.205 1.035 0.828 0.604 0.373 0.134 -0.128 -0.441 -0.852 -1.445 -2.372 -3.911 -6.536 -10.749 -15.049 -17.334 -13.947 0. ]
[ 0. 0.34 0.609 0.755 0.751 0.618 0.516 0.411 0.299 0.183 0.059 -0.081 -0.253 -0.491 -0.859 -1.499 -2.766 -5.652 -13.307 -22.167 -30.267 -31.497 0. ]
[ 0. 0.203 0.357 0.416 0.331 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -14.659 -30.045 -50.068 -81.776 -150. ]
[ 0. 0.116 0.2 0.221 0.157 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -15.284 -33.286 -58.184 -95.538 -150. ]
[ 0. 0.062 0.104 0.112 0.075 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -13.192 -29.633 -53.842 -92.192 -150. ]
[ 0. 0.027 0.044 0.047 0.03 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -7.851 -18.211 -35.36 -69.388 -150. ]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. -150. ]]
我遇到的问题是,渲染的默认精度不会显示中心值之间的差异,这对我的应用程序而言最重要:
由于我的值介于150到-150之间,因此matplotlib为所有中间值([-5,5])赋予相同的颜色
我想控制缩放比例以生成某种热图,以显示整个图形的进度。
相关的python代码:
plt.matshow(mat, aspect='auto', cmap=plt.get_cmap('magma'))
plt.show()
达到此结果的最简单方法是什么?
答案 0 :(得分:3)
您可以对此进行调整:
import matplotlib.pyplot as plt
import matplotlib
mat = [[ 150. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ],
[ 150. , 69.388 , 35.36 , 18.211, 7.851, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , -0.03 , -0.047 , -0.044 , -0.027 , 0. ],
[ 150. , 92.192 , 53.842 , 29.633,13.192, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , -0.075 , -0.112 , -0.104 , -0.062 , 0. ],
[ 150. , 95.538 , 58.184 , 33.287,15.285, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , -0.156 , -0.221 , -0.199 , -0.116 , 0. ],
[ 150. , 81.776 , 50.068 , 30.045,14.659, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , -0.331 , -0.416 , -0.357 , -0.203 , 0. ],
[ 0. , 31.498 , 30.267 , 22.168,13.308, 5.653, 2.766 , 1.5 , 0.86 , 0.492 , 0.254 , 0.082, -0.058, -0.182 , -0.298 , -0.41 , -0.515 , -0.617, -0.751 , -0.754 , -0.609 , -0.34 , 0. ],
[ 0. , 13.948 , 17.335 , 15.05 ,10.751, 6.537, 3.913 , 2.374 , 1.447 , 0.854 , 0.443 , 0.13 , -0.132, -0.372 , -0.602 , -0.826 , -1.034 , -1.204, -1.301 , -1.241 , -0.985 , -0.548 , 0. ],
[ 0. , 6.958 , 10.074 , 9.949, 8.107, 5.832, 3.973 , 2.635 , 1.7 , 1.034 , 0.534 , 0.129, -0.229, -0.57 , -0.913 , -1.258 , -1.59 , -1.864, -2.007 , -1.924 , -1.543 , -0.866 , 0. ],
[ 0. , 3.812 , 6.053 , 6.564, 5.896, 4.712, 3.511 , 2.493 , 1.685 , 1.048 , 0.529 , 0.079, -0.342, -0.768 , -1.22 , -1.705 , -2.203 , -2.654, -2.941 , -2.904 , -2.395 , -1.375 , 0. ],
[ 0. , 2.237 , 3.761 , 4.358, 4.202, 3.609, 2.867 , 2.142 , 1.498 , 0.943 , 0.455 , 0.002, -0.452, -0.94 , -1.495 , -2.139 , -2.864 , -3.607, -4.2 , -4.357 , -3.76 , -2.237 , 0. ],
[ 0. , 1.375 , 2.396 , 2.906, 2.943, 2.656, 2.206 , 1.708 , 1.223 , 0.771 , 0.345 ,-0.076, -0.526, -1.045 , -1.682 , -2.491 , -3.509 , -4.71 , -5.895 , -6.563 , -6.052 , -3.812 , 0. ],
[ 0. , 0.867 , 1.543 , 1.925, 2.009, 1.865, 1.592 , 1.261 , 0.915 , 0.573 , 0.232 ,-0.126, -0.531, -1.031 , -1.698 , -2.633 , -3.971 , -5.831, -8.105 , -9.948 , -10.073 , -6.958 , 0. ],
[ 0. , 0.548 , 0.986 , 1.242, 1.302, 1.205, 1.035 , 0.828 , 0.604 , 0.373 , 0.134 ,-0.128, -0.441, -0.852 , -1.445 , -2.372 , -3.911 , -6.536, -10.749 , -15.049 , -17.334 ,-13.947 , 0. ],
[ 0. , 0.34 , 0.609 , 0.755, 0.751, 0.618, 0.516 , 0.411 , 0.299 , 0.183 , 0.059 ,-0.081, -0.253, -0.491 , -0.859 , -1.499 , -2.766 , -5.652, -13.307 , -22.167 , -30.267 ,-31.497 , 0. ],
[ 0. , 0.203 , 0.357 , 0.416, 0.331, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , -14.659 , -30.045 , -50.068 ,-81.776 ,-150. ],
[ 0. , 0.116 , 0.2 , 0.221, 0.157, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , -15.284 , -33.286 , -58.184 ,-95.538 ,-150. ],
[ 0. , 0.062 , 0.104 , 0.112, 0.075, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , -13.192 , -29.633 , -53.842 ,-92.192 ,-150. ],
[ 0. , 0.027 , 0.044 , 0.047, 0.03 , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , -7.851 , -18.211 , -35.36 ,-69.388 ,-150. ],
[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,-150. ]]
plt.matshow(mat, aspect='auto', norm=matplotlib.colors.SymLogNorm(0.1))
plt.show()
将产生以下图像:
说明:
在matplotlib
中,可以使用归一化的选项,当您处理相对较大的数字时,而较小的数字则更为重要,因此您需要记录(即对数)值以查看差异。因为您也有负数,只是记录它们不会减少它(log()显然不会处理负数)。因此,SymLogNorm
也以相同形式处理负数。
0.1
是一个重要的参数,因为接近零的值趋于无穷大,因此需要在零附近具有线性范围。参数linthresh({我在这里通过查找矩阵值在这里选择的0.1
)允许用户指定此范围的大小(-linthresh,linthresh)。
有关更多详细信息:SymLogNorm documentation