2017-05-08 105 views
1

我想在矩陣中讀取圖中的特定單元格的方向。如何使用'乳膠箭頭符號'在圖中創建對角箭頭?

在附圖中,我可以創建左方向箭頭,但我沒有得到如何在西南方向插入對角箭頭。

enter image description here

顯然,latex arrow symbol工作正常的左箭頭,右箭頭,箭頭向上和向下箭頭,但未能爲斜箭頭。

在附圖中我需要通過青色細胞向西南方向插入箭頭。這個怎麼做?

這裏是我想劇本,

for row=1:size(data,1) 
    for col=1:size(data,2); 

     if data(row,col)==1 
      rectangle('Position',[col-0.5 row 1 1], 'FaceColor','y','EdgeColor','k', 'LineWidth', 0.1) 
      text(col-0.95,row+0.6,'\leftarrow', 'fontsize', 6); 

     elseif data(row,col)==2 
      rectangle('Position',[col-0.5 row 1 1], 'FaceColor','c','EdgeColor','k', 'LineWidth', 0.1) 
      text(col-0.95,row+0.6,'\swarrow', 'fontsize', 6); 

     else 
      rectangle('Position',[col-0.5 row 1 1], 'FaceColor','w','EdgeColor','k', 'LineWidth', 0.1)    
     end 

    end 

    set(gca,'Visible','off') 
end 

編輯: Annotation選項會增加複雜性,需要爲每一個問題箭頭的位置。如果可以使用乳膠箭頭符號,問題會變得更容易。

+0

你考慮到剛剛繪製矢量而不是使用Latex文本呢? – Bernhard

+0

不,我不知道。你能再詳細一點嗎? – Mario

+0

'annotation'可以做這項工作 –

回答

1

如上所示,您可以使用quiver來繪製箭頭。此外,pcolor是繪製所有矩形併爲它們着色的更好方法。

下面是使用他們都創造一些像你想要什麼代碼的例子:

data = randi(3,10)-1; % some random data 
% plot the rectangles: 
pcolor([data data(:,end); 
     data(end,:) 0]) 
% set the colors to w-y-c: 
colormap([1 1 1; 
    1 1 0; 
    0 1 1]); 
[r, c] = ndgrid(1:size(data,1),1:size(data,2)); % a grid of all the cells 
% logical indexing for the arrows: 
leftarrow = (data==1); 
swarrow = (data==2); 
% plot all the arrows in black: 
hold on 
quiver([r r]+0.1,[c c]+0.5,[-leftarrow.' -swarrow.'],... 
    [zeros(size(data)) -swarrow.'],'AutoScaleFactor',0.5,'Color','k') 
hold off 
set(gca,'Visible','off') 

和典型的結果是: pcolor & quiver