Matlab⽤不同的数值表⽰不同的颜⾊——可视化不同⾊块
function [x,y,z] = frequency_visualization(tick_x,tick_y,tick_z,x_n,y_n,V)
% 此程序是对⽴⽅体中存储轨迹数据的频数进⾏可视化
% meshgrid ⽣成三维⽹格
[x,y,z] = meshgrid(tick_x,tick_y,tick_z);
% % 三维体切⽚平⾯
% cube = slice(x,y,z,z,tick_x,tick_y,tick_z);
% ⽤cat函数读取⼀列
frequency = cat(1,V.points);
% 最⼤频数
max_frequency = max(frequency);
% 控制不同频数的⼩⽴⽅体的颜⾊变化
% 控制颜⾊变化的条应该是10份(⼏份除以⼏)
% unit = max_frequency/10;
% 调⽤str_split函数
all_digital = str_split(V);
%%%%%%%%%%%%%%%% 显⽰三维⽴⽅体部分代码
% 按z轴进⾏切⽚,因为z轴长度较短,容易切⽚
for i =1: length(tick_z)
% 打开⼀个新窗⼝
% figure;
% 字符串匹配
% temp_i = num2str(i);
% 索引
index = [];
% 检查此切⾯中的⼩⽴⽅体的频数,返回存储这些频数的索引index
for k = 1:length(all_digital)
if all_digital(k) ==i
index = [index k];
else
continue
end
end
% 取出这个切⾯中存储点的所有⼩⽴⽅体和其频数
segment = V(index);
% 将有频数的⼩⽴⽅体上⾊,频数越⼤越⿊
% 创建矩阵
map = zeros(x_n-1,y_n-1);
for j = 1:length(segment)
% 将频数值存⼊相应的矩阵中
a = segment(j).shape;
b = strsplit(a,'*');
c = str2num(b{1,1});
d = str2num(b{1,2});
% 赋值
map(c,d) = segment(j).points;
map(c,d) = segment(j).points;
end
% 归⼀化数值
nor_map = abs(log(map / max_frequency));
nor_map(nor_map == inf) =0;
[col,row] = size(nor_map);
temp_map = reshape(nor_map,[1,col * row]);
tmp_max = max(temp_map);
nor_map = nor_map / tmp_max;
nor_map = 1-nor_map;
% 将⽤数字表⽰颜⾊深浅的归⼀化⼆维矩阵nor_map显⽰到三维空间
%%%%%%%% 重要的显⽰部分
% nor_map = 1-nor_map;
%画出可视化数据
% matrixplot(nor_map,'XVarNames',XVarNames,'YVarNames',YVarNames,'DisplayOpt','off');
matrixplot(nor_map,'DisplayOpt','off');
colormap(gray(256))
%    % 进⾏横切⾯操作
%    xslice = [];
%    yslice = [];
%    zslice =tick_z(i) ;
%    v = x.*exp(-x.^2-y.^2-z.^2);
%    % 切割并显⽰
%    slice(x,y,z,v,xslice,yslice,zslice);
% 等⽐设置
axis equal
% axis设置轴范围和纵横⽐,off为取消对坐标轴的⼀切设置,on为开启
axis off
% 需要修改
% 需要进⾏字符串设定
name =  sprintf('%d.png',i);
% 按name的⽂件名保存当前窗⼝
saveas(gcf,name);
end
% % 显⽰,设置图形对象属性,FaceColor是⾯的颜⾊,Marker顶点⽤什么表⽰
% % set(cube,'FaceColor','none','Marker','o','MarkerFaceColor','y')
% set(cube,'FaceColor','none','Marker','none')
% % view视点的指定
% view(3)
end
上⾯是可视化的代码,其中需要调⽤两个函数str_split(此函数传⼊的参数是⼀个结构体)和matrixplot(传⼊的参数是数据矩阵)
function matrixplot(data,varargin)
%  根据实值矩阵绘制⾊块图,⽤丰富的颜⾊和形状形象的展⽰矩阵元素值的⼤⼩。
%
%  matrixplot(data) 绘制矩阵⾊块图,data为实值矩阵,每⼀个元素对应⼀个⾊块,⾊
%                    块颜⾊由元素值⼤⼩决定。
%
%  matrixplot(data, 'PARAM1',val1, 'PARAM2',val2, ...)
%          ⽤成对出现的参数名/参数值控制⾊块的各项属性。可⽤的参数名/参数值如下:
%          'FigShape' --- 设定⾊块的形状,其参数值为:
%                'Square'  --- ⽅形(默认)
%                'Circle'  --- 圆形
%                'Ellipse' --- 椭圆形
%                'Hexagon' --- 六边形
%                'Dial'    --- 表盘形
%
%          'FigSize' --- 设定⾊块的⼤⼩,其参数值为:
%                'Full'    --- 最⼤⾊块(默认)
%                'Auto'    --- 根据矩阵元素值⾃动确定⾊块⼤⼩
%
%          'FigStyle' --- 设定矩阵图样式,其参数值为:
%                'Auto'    --- 矩形矩阵图(默认)
%                'Tril'    --- 下三⾓矩阵图
%                'Triu'    --- 上三⾓矩阵图
%
%          'FillStyle' --- 设定⾊块填充样式,其参数值为:
%                'Fill'    --- 填充⾊块内部(默认)
%                'NoFill'  --- 不填充⾊块内部
%
%          'DisplayOpt' --- 设定是否在⾊块中显⽰矩阵元素值,其参数值为:
%                'On'      --- 显⽰矩阵元素值(默认)
%                'Off'    --- 不显⽰矩阵元素值
%
%          'TextColor' --- 设定⽂字的颜⾊,其参数值为:
%                表⽰单⾊的字符('r','g','b','y','m','c','w','k'),默认为⿊⾊
%                1⾏3列的红、绿、蓝三元⾊灰度值向量([r,g,b])
%                'Auto'    --- 根据矩阵元素值⾃动确定⽂字颜⾊
%
%          'XVarNames' --- 设定X轴⽅向需要显⽰的变量名(默认为X1,X2,...),其参数值为:%                字符串矩阵或字符串元胞数组,若为字符串矩阵,其⾏数应与data的列数相同%                若为字符串元胞数组,其长度应与data的列数相同。
%
%          'YVarNames' --- 设定Y轴⽅向需要显⽰的变量名(默认为Y1,Y2,...),其参数值为:%                字符串矩阵或字符串元胞数组,若为字符串矩阵,其⾏数应与data的⾏数相同%                若为字符串元胞数组,其长度应与data的⾏数相同。
%
%          'ColorBar' --- 设定是否显⽰颜⾊条,其参数值为:
%                'On'      --- 显⽰颜⾊条
%                'Off'    --- 不显⽰颜⾊条(默认)
%
%          'Grid' --- 设定是否显⽰⽹格线,其参数值为:
%                'On'      --- 显⽰⽹格线(默认)
%                'Off'    --- 不显⽰⽹格线
%
%  Example:
%  x = [1,-0.2,0.3,0.8,-0.5
%        -0.2,1,0.6,-0.7,0.2
%        0.3,0.6,1,0.5,-0.3
%        0.8,-0.7,0.5,1,0.7
%        -0.5,0.2,-0.3,0.7,1];
%  matrixplot(x);
%  matrixplot(x,'DisplayOpt','off');
%  matrixplot(x,'FillStyle','nofill','TextColor','Auto');
%  matrixplot(x,'TextColor',[0.7,0.7,0.7],'FigShap','s','FigSize','Auto','ColorBar','on');
%  matrixplot(x,'TextColor','k','FigShap','d','FigSize','Full','ColorBar','on','FigStyle','Triu'); %  XVarNames = {'xiezhh','heping','keda','tust','tianjin'};
%  XVarNames = {'xiezhh','heping','keda','tust','tianjin'};
%  matrixplot(x,'FigShap','e','FigSize','Auto','ColorBar','on','XVarNames',XVarNames,'YVarNames',XVarNames); % 对第⼀个输⼊参数类型进⾏判断
if ~ismatrix(data) || ~isreal(data)
error('输⼊参数类型不匹配:第⼀个输⼊参数应为实值矩阵');
end
% 解析成对出现的参数名/参数值
[FigShape,FigSize,FigStyle,FillStyle,DisplayOpt,TextColor,XVarNames,...
YVarNames,ColorBar,GridOpt] = parseInputs(varargin{:});
% 产⽣⽹格数据
[m,n] = size(data);
[x,y] = meshgrid(0:n,0:m);
data = data(:);
maxdata = nanmax(data);
mindata = nanmin(data);
rangedata = maxdata - mindata;
if isnan(rangedata)
warning('MATLAB:warning1','请检查您输⼊的矩阵是否合适!');
return;
end
z = zeros(size(x))+0.2;
sx = x(1:end-1,1:end-1)+0.5;
sy = y(1:end-1,1:end-1)+0.5;
if strncmpi(FigStyle,'Tril',4)
z(triu(ones(size(z)),2)>0) = NaN;
sx(triu(ones(size(sx)),1)>0) = NaN;
elseif strncmpi(FigStyle,'Triu',4)
z(tril(ones(size(z)),-2)>0) = NaN;
sx(tril(ones(size(sx)),-1)>0) = NaN;
end
sx = sx(:);
sy = sy(:);
id = isnan(sx) | isnan(data);
sx(id) = [];
sy(id) = [];
data(id) = [];
if isempty(XVarNames)
XVarNames = strcat('X',cellstr(num2str((1:n)')));
else
if (iscell(XVarNames) && (numel(XVarNames) ~= n)) || (~iscell(XVarNames) && (size(XVarNames,1) ~= n))        error('X轴⽅向变量名应为字符串矩阵或字符串元胞数组,其长度与输⼊矩阵的列数相同');
end
end
if isempty(YVarNames)
YVarNames = strcat('Y',cellstr(num2str((1:m)')));
fontweight属性bold
else
if (iscell(YVarNames) && (numel(YVarNames) ~= m)) || (~iscell(YVarNames) && (size(YVarNames,1) ~= m))        error('Y轴⽅向变量名应为字符串矩阵或字符串元胞数组,其长度与输⼊矩阵的⾏数相同');
end
end
% 绘图
figure('color','w',...
'units','normalized',...
'pos',[0.289165,0.154948,0.409956,0.68099]);
axes('units','normalized','pos',[0.1,0.022,0.89,0.85]);
if strncmpi(GridOpt,'On',2)
mesh(x,y,z,...
'EdgeColor',[0.7,0.7,0.7],...
'FaceAlpha',0,...
'LineWidth',1);  % 参考⽹格线
end
end
hold on;
axis equal;
axis([-0.1,n+0.1,-0.1,m+0.1,-0.5,0.5]); view(2);
% 设置X轴和Y轴刻度位置及标签
set(gca,'Xtick',(1:n)-0.5,...
'XtickLabel',XVarNames,...
'Ytick',(1:m)-0.5,...
'YtickLabel',YVarNames,...
'XAxisLocation','top',...
'YDir','reverse',...
'Xcolor',[0.7,0.7,0.7],...
'Ycolor',[0.7,0.7,0.7],...
'TickLength',[0,0]);
axis off
% 绘制填充⾊块
if strncmpi(FillStyle,'Fill',3)
MyPatch(sx',sy',data',FigShape,FigSize); end
% 显⽰数值⽂本信息
if strncmpi(DisplayOpt,'On',2)
str = num2str(data,'%4.2f');
scale = 0.1*max(n/m,1)/(max(m,n)^0.55);    if strncmpi(TextColor,'Auto',3)
ColorMat = get(gcf,'ColorMap');
nc = size(ColorMat,1);
cid = fix(mapminmax(data',0,1)*nc)+1;        cid(cid<1) = 1;
cid(cid>nc) = nc;
TextColor = ColorMat(cid,:);
for i = 1:numel(data)
text(sx(i),sy(i),0.1,str(i,:),...
'FontUnits','normalized',...
'FontSize',scale,...
'fontweight','bold',...
'HorizontalAlignment','center',...                'Color',TextColor(i,:));
end
else
text(sx,sy,0.1*ones(size(sx)),str,...
'FontUnits','normalized',...
'FontSize',scale,...
'fontweight','bold',...
'HorizontalAlignment','center',...
'Color',TextColor);
end
end
% 设置X轴和Y轴刻度标签的缩进⽅式MyTickLabel(gca,FigStyle);
% 添加颜⾊条
if strncmpi(ColorBar,'On',2)
if any(strncmpi(FigStyle,{'Auto','Triu'},4))        colorbar('Location','EastOutside');
else
colorbar('Location','SouthOutside');
end
end
end
% ---------------------------------------------------%  调整坐标轴刻度标签⼦函数

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。