2017-04-25 171 views
1

我正在繪製節點與treeplot(nodes),我想用圖像替換節點符號(默認爲圓形)。繪製樹狀結構與圖像作爲節點在matlab中

在MATLAB中treeplot功能的工作原理與以下圖形表示法:

nodes = [0,1,1,2,2] 

其中元件的位置是節點號,該值表示父節點。示例節點1是根,所以值爲0.節點2和3是節點1的子節點,類似於它繼續。

當通過這個載體的funtion之一MATLAB

treeplot(nodes) 

我們得到了一個樹狀結構:

output of treeplot(nodes) with nodes = [0,1,1,2,2]

在這個情節我想要的圖像,而不是節點符號(圓圈)。我怎樣才能做到這一點?

回答

0

您可以使用[x,y] = treelayout(nodes)以獲得和y座標節點的座標treeplot(nodes)。然後用hold onimage來繪製你的情節。

以下示例使用MATLAB示例圖像並將圖像大小調整爲圖的5%。圖像以節點爲中心(0.5*size multiplication添加到座標)並添加標籤(使用text)。

img = imread('peppers.png'); % MATLAB demo image 
sizeX = 0.05; sizeY = 0.05;  % Define image size in plot 
[x,y] = treelayout(nodes);  % get x,y of nodes in plot 

nodes = [0,1,1,2,2]; 
treeplot(nodes);       
hold on;      % add to your treeplot 

for i=1:length(x)    % for all nodes do: 

    % draw image centered at node 
    image([x(i)-0.5*sizeX x(i)+0.5*sizeX], ... 
      [y(i)+0.5*sizeY y(i)-0.5*sizeY],img); 

    % optional: label 
    text(x(i),y(i),num2str(i),'Color','white'); 

end 

Resulting image of code: Treeplot with images and labels