Matlab数据可视化(5):二维数据可视化 I
2018-12-03
以下介绍数据的二维可视化。
一. 二维散点图
(源代码:scatter.m)
我们用著名的Iris数据集(Fisher, 1936)作为绘图实例。Iris数据集包含3种鸢尾花的150个样本数据,每个数据都有4个属性(花萼和花瓣的长度及宽度)。
1) 基本散点图
我们用其中两个属性值作为X和Y轴,另一个属性值表示点的大小。(图1)
[plain]view plaincopy
%% 加载数据集
[attrib className] = xlsread("iris.xlsx");
%% 绘制基本的散点图
figure("units","normalized","Position",[0.2359 0.3009 0.4094 0.6037]);
scatter(attrib(:,1),attrib(:,2),10*attrib(:,3),[0 1 0],"filled","Marker","^");
set(gca,"Fontsize",12);
title({"Iris数据集包含150个数据,每个数据含4个属性",...
"第32个属性值扩大后用来决定标志的大小",...
"标志使用自定义的大小和样式"});
xlabel("属性1"); ylabel("属性2");
box on;
set(gcf,"color",[1 1 1],"paperpositionmode","auto");
图 1
2) 散点矩阵图
plotmatrix可以将每两个属性组合的散点图以矩阵的形式绘制出来。通过观察如图2,我们知道,3个各类的花朵不能通过某两个属性就可以加以区分。
[plain]view plaincopy
%% 绘制散点矩阵图
% The output parameters have
% A matrix of handles to the objects created in H,
% A matrix of handles to the individual subaxes in AX,
% A handle to a big (invisible) axes that frames the subaxes in BigAx,
% A matrix of handles for the histogram plots in P.
% BigAx is left as the current axes so that a subsequent title, xlabel, or ylabel command is centered with respect to the matrix of axes.
figure("units","normalized","Position",[ 0.2359 0.3009 0.4094 0.6037]);
[H,AX,BigAx,P] = plotmatrix(attrib,"r.");
attribName = {[char(10) "花萼长度"],[char(10) "花萼宽度"],[char(10) "花瓣长度"],[char(10) "花瓣宽度"]};
% 添加标注
for i = 1:4
set(get(AX(i,1),"ylabel"),"string",["属性" num2str(i) attribName{i}]);
set(get(AX(4,i),"xlabel"),"string",["属性" num2str(i) attribName{i}]);
end
set(get(BigAx,"title"),"String",{"散点矩阵图 (数据含3类Iris花)", ...
"如图所示这3类不能通过某2个属性进行区分"},"Fontsize",14);
set(gcf,"color",[1 1 1],"paperpositionmode","auto");
图 2
3) 带直方图的散点图
我们可以在绘制两个属性的散点图的同时,绘制出各属性分布的直方图。(图3)
[plain]view plaincopy
%% 在轴线上画出分布图
figure("units","normalized","Position",[0.2589 0.3296 0.3859 0.5750]);
H = scatterhist(attrib(:,2),attrib(:,3),"nbins",50,"direction","out");
mainDataAxes = H(1);
xhistAxes = H(2);
yhistAxes = H(3);
set(get(mainDataAxes,"title"),"String","有一个类别基于如下2个属性是可分的","Fontsize",12);
set(get(mainDataAxe