在MATLAB中的正方形图

时间:2015-05-04 07:56:28

标签: matlab matrix matlab-figure

我有一个包含7行的矩阵,我自动上传并生成图表。我无法弄清楚,我怎么能在一个正方形中完全代表它。矩阵制作的图表如下所示:

graph

这是显示我文件中的矩阵的代码:

parsing of javascript

如何将矩阵制作的图形放在白色方块的位置?

enter image description here

这是从文件

创建矩阵的代码
reverse_matrix = flipud(matrix_to_display)
imagesc(reverse_matrix) 
set(gca,'XTickLabel',{'2','4', '6', '8', '10', '12', '14'})
set(gca,'YTickLabel',{'33.60 cm','29.25 cm', '24.90 cm', '20.55 cm', '16.20 cm', '11.85 cm', '7.50 cm'})

2 个答案:

答案 0 :(得分:2)

Hwathanie暗示的解决方案实际上是最简单的方法,如果您只有1个矩形的图像数据可供绘图,如果您可以通过重新调整XY刻度标签来显示你想要在轴标签上。

在他纠正他的代码之前,如果你想让它工作,可以用他的第一个代码和他的第二个代码玩一下(看看我对他的回答的评论,所有你应该到达的地方)。

我采用了另一种更复杂的方法,但具有更高的可扩展性。基本上我创建一个网格补丁,其面数和矩阵中的点数一样多,然后我为每个面部着色,因为imagesc会为图像着色。这个patch对象具有可移动和可扩展的优点,因此我可以直接将其插入到您想要的“矩形”的位置,就像您之前的question

一样
%// sample matrix data
nl = 7 ; nc = 14 ; nColor = 16 ;
cmap = gray(nColor) ;                   %// Pick a gray colormap (16 colors)
matrix_to_display = rand( nl , nc ) ;   %// random colors matrix
reverse_matrix = flipud(matrix_to_display);
figure ; colormap(cmap) ;               %// assign the gray colormap to the figure

%// Get the hatched background
xlim = [0 61] ; ylim = [0 45] ;
[X,Y] = hatch_coordinates( xlim , ylim ) ; %// this return coordinates to plot a hatch pattern
plot(X,Y,'k')                              %// and this simply plot the pattern, with the attributes you want (color, linespec, etc ...)
hold on ; grid off

%// *** THIS IS THE INTERESTING BIT ***
%// Define your rectangle patch 
pos = [3.75 5.6 53.5 29.5];  %// [x0 y0 W H] of your rectangle

fv = meshpatch( size(reverse_matrix) ) ;                            %// get the basic patch face grid
fv.vertices = bsxfun( @times , fv.vertices , [pos(3) pos(4) 1] ) ;  %// apply scale factor to vertices coordinates
fv.vertices = bsxfun( @plus , fv.vertices  , [pos(1) pos(2) 0] ) ;  %// apply offset to vertices coordinates

PatchFacesColors = rot90(reverse_matrix,-1) ;                       %// necessary to adjust color index to patch faces organisation
fv.facevertexcdata = PatchFacesColors(:) ;                          %// add "facevertexcdata" to the patch structure
hp = patch(fv,'FaceColor','flat','CDataMapping','scaled','EdgeColor','none') ; %// create the patch

%// refine axis
axis([0 61 0 45])
yt = [0 7.5 11.85 16.2 20.55 24.9 29.25 33.6 39.3 45].' ;
set( gca , 'Ytick' , yt , 'YTickLabel' , num2str(yt) )

griddedpatch

CODE:

对于函数hatch_coordinates.m,可在此答案中找到:Hatch a plot in MATLAB

函数meshpatch.m

是:

function fv = meshpatch( meshdim , normalized )
%// function fv = meshpatch( meshdim , normalised )
%//
%// return patch structure f.faces and f.vertices of a patch having the same
%// number of faces than the input size.

if nargin < 2 ; normalized = true ; end %// default option

nfx = meshdim(2) ; %// number of faces on X
nfy = meshdim(1) ; %// number of faces on X

%% // build patch vertices
x = (0:nfx).' * ones(1,nfy+1) ;    %'//ignore this comment
y = ones(nfx+1,1) * (0:nfy) ;
if normalized
    x = x ./ nfx ;
    y = y ./ nfy ;
end
fv.vertices = [x(:) y(:) zeros(numel(x),1) ] ;

%% // build patch faces
f0 = [1 2 nfx+3 nfx+2].' ;              %'// basic patch cell
fl = f0 * ones(1,nfx*nfy) ;             %// replicate to create all patch cell

%// now adjust the vertex indices to get all the cells rigth
cellAdd = bsxfun(@plus,(1:nfx).'*ones(1,nfy),(0:nfx+1:nfy*nfx))-1 ; 
fv.faces = bsxfun(@plus,fl, cellAdd(:).' ).' ; %// adjust indices of patch cell

答案 1 :(得分:1)

如下所示的简单解决方法呢?

matrix_to_display = ones(9,9,3);
a = rand(7,7,3);
matrix_to_display(2:8,2:8,:) = a;

reverse_matrix = flipud(matrix_to_display);
imagesc(reverse_matrix); 

set(gca,'XTickLabel',{'2','4', '6', '8', '10', '12', '14'});
set(gca,'YTickLabel',{'33.60 cm','29.25 cm', '24.90 cm', '20.55 cm', '16.20 cm', '11.85 cm', '7.50 cm'});

输出:

enter image description here

<强>更新

检查以下代码。

clear all;

matrix_to_display = rand(7,7,3);

xlim = [0 8] ;
ylim = [0 8] ;
[X,Y] = hatch_coordinates( xlim , ylim , .1, .1) ;
plot(X,Y,'k');
hold on ; grid off

reverse_matrix = flipud(matrix_to_display);
img = image(reverse_matrix); 

p = patch([.5 7.5 7.5 .5],[.5 0.5 7.5 7.5],'r');
set(p,'FaceAlpha',0);

输出:

enter image description here

更新2:

考虑到Hoki的建议,我更新了以下代码。

clear all;

matrix_to_display = rand(7,7);

xlim = [0 8] ;
ylim = [0 8] ;
[X,Y] = hatch_coordinates( xlim , ylim , .1, .1) ;
plot(X,Y,'k');
hold on ; grid off

reverse_matrix = flipud(matrix_to_display);
img = imagesc(reverse_matrix); 
colormap('gray');

p = patch([.5 7.5 7.5 .5],[.5 0.5 7.5 7.5],'r');
set(p,'FaceAlpha',0);

set(gca,'XTickLabel',{'1', '2','4', '6', '8', '10', '12', '14', '15'})
set(gca,'YTickLabel',{'37.95 cm','33.60 cm','29.25 cm', '24.90 cm', '20.55 cm', '16.20 cm', '11.85 cm', '7.50 cm', '3.15 cm'})

输出:

enter image description here