将两个矩阵绘制为同一图表上的色彩映射

时间:2018-03-25 11:19:31

标签: python numpy matplotlib

我有两个numpy多维矩阵,每个矩阵都有五个这样的特征

 array1 = array([ 1. ,  0.97572023,  0.97671645,  0.99772446,
    0.99326534,  0.94841498]....) 
 array2 = array([ 0.97572023,  1.        ,  0.99343976,  0.9844228 ,
    0.9880037 ,  0.96203135]....) 

我想将这些多维矩阵绘制为彩色图并在图形上标记每个特征。这是绘制多维数组的最佳方法。

from matplotlib import pyplot as plt
from matplotlib import cm as cm

fig = plt.figure()
ax1 = fig.add_subplot(111)
cmap = cm.get_cmap('jet', 30)
cax = ax1.imshow(df, interpolation="nearest", cmap=cmap)
ax1.grid(True)
plt.title('Abalone Feature Correlation')
labels=['feat1','feat2','feat3','feat4','feat5']
ax1.set_xticklabels(labels,fontsize=6)
ax1.set_yticklabels(labels,fontsize=6)
# Add colorbar, make sure to specify tick locations to match desired ticklabels
fig.colorbar(cax, ticks=[0.1,0.2,0.3,0.4,0.5,0.6,.75,.8,.85,.90,.95,1])
plt.show()

我正在使用此功能,但功能未正确显示..标签和点未正确显示。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

指示matplotlib使用imshow图表的特定刻度确保标签出现在正确的位置,

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm as cm

# Generate some data for the sake of example
array = np.random.uniform(0, 1, (5, 5))

fig = plt.figure()
ax1 = fig.add_subplot(111)
cmap = cm.get_cmap('jet', 30)


cax = ax1.imshow(array, interpolation="nearest", cmap=cmap)
ax1.grid(True)
plt.title('Abalone Feature Correlation')
labels=['feat1', 'feat2', 'feat3', 'feat4', 'feat5']

# Explicitly set ticks for the plot
ax1.set_xticks(np.arange(len(labels)))
ax1.set_yticks(np.arange(len(labels)))
ax1.set_xticklabels(labels,fontsize=6)
ax1.set_yticklabels(labels,fontsize=6)

# Add colorbar, make sure to specify tick locations to match desired ticklabels
fig.colorbar(cax, ticks=[0.1,0.2,0.3,0.4,0.5,0.6,.75,.8,.85,.90,.95,1])
plt.show()

Array.ofSize(...)