【问题标题】:Plot tree like structure with images as nodes in matlab在matlab中以图像为节点绘制树状结构
【发布时间】:2017-04-26 03:37:04
【问题描述】:

我正在用treeplot(nodes) 绘制节点,我想用图像替换节点符号(默认为圆圈)。

Matlab 中的树形图函数使用以下图形符号:

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

其中元素的位置是节点号,值代表父节点。示例节点 1 是根,因此值为 0。节点 2 和 3 是节点 1 的子节点,类似地继续。

当将此向量传递给 matlab 中的某个函数时

treeplot(nodes)

我们得到一个树状结构:

在这个情节中,我想要图像而不是节点符号(圆圈)。我该怎么做?

【问题讨论】:

    标签: matlab graph


    【解决方案1】:

    您可以使用[x,y] = treelayout(nodes) 获取treeplot(nodes) 中节点的x- 和y- 坐标。然后使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 2012-07-15
      相关资源
      最近更新 更多