最近在学习一些线性相关分析的一些知识,学到Person相关系数,觉得比较有意思,下面给出常见的相关性分析用到的利用Matlab绘制相关性矩热力图的代码示例,供大家参考。
在计算数据之间的Person相关性时,经常会使用MATLAB绘制相关性矩阵热力图,能够通过分析热力图,更好地去分析数据之间的关联性。
以下是绘制一个8*8的热力图的详细代码,修改颜色的话,可以修改mycolor数组的数值。
mycolor = [0.474509803921569,0.650980392156863,0.807843137254902;...
0.682352941176471,0.823529411764706,0.898039215686275;...
0.941176470588235,0.972549019607843,0.862745098039216;...
0.992156862745098,0.968627450980392,0.705882352941177;...
1,0.901960784313726,0.603921568627451];
matrix = [0.999,0.911,0.995,0.984,0.995,1.000,0.938,1.000
0.946,0.896,0.949,0.911,0.950,0.941,1.000,0.938
0.999,0.912,0.996,0.983,0.996,1.000,0.950,0.995
0.998,0.918,1.000,0.985,1.000,0.996,0.950,0.995
0.985,0.900,0.987,1.000,0.985,0.983,0.911,0.984
0.998,0.918,1.000,0.987,1.000,0.996,0.949,0.995
0.919,1.000,0.918,0.900,0.918,0.912,0.896,0.911
1.000,0.919,0.998,0.985,0.998,0.999,0.946,0.999];
%% 开始绘制热图
label_name = {'建筑消费部门','交通消费部门','第三产业生产总量','工业消费部门','能源供应部门','第二产业生产总量','第一产业生产总量','GDP总量'};
xlabel_name = label_name;
ylabel_name = {'GDP总量','第一产业生产总量','第二产业生产总量','能源供应部门','工业消费部门','第三产业生产总量','交通消费部门','建筑消费部门'};
% 热图函数为heatmap;开始绘制
hot_figure = heatmap(xlabel_name,ylabel_name,matrix,'FontSize',18);
% hot_figure.GridVisible = 'off';
% hot_figure.Title = 'Heated';
% hot_figure.XLabel = 'xlabel';
% hot_figure.YLabel = 'ylabel';
%% 对热图上色—colormap函数
colormap(gca,mycolor);
下图是热力图: