matlab - 如何在Matlab中画出这种树状的点阵结构?

标签 matlab plot tree structure

我要绘制的结构应该与显示的结构完全一样。谢谢! enter image description here

最佳答案

这是我快速创建的一个小函数,它将一个正方形上三角矩阵作为输入,并绘制如上所示的晶格结构:

function hFigure = plot_lattice(A)

  %# Compute all the coordinates needed for the lines and points:

  N = size(A,1);
  [xPoints,yPoints] = meshgrid(0:N-1);
  yPoints = bsxfun(@plus,-yPoints,0:0.5:(N-0.5)/2);
  xLines = [xPoints([1:N+1:N^2-N-1 1:N:N^2-2*N+1]); ...
            xPoints([1:N-1 N:-1:2],N).'];  %'
  yLines = [yPoints([1:N+1:N^2-N-1 1:N:N^2-2*N+1]); ...
            yPoints([1:N-1 N:-1:2],N).'];  %'
  index = find(triu(reshape(1:N^2,N,N)));
  xPoints = xPoints(index);
  yPoints = yPoints(index);
  values = strtrim(cellstr(num2str(A(index))));

  %# Create the figure:

  hFigure = figure('Color','w');
  hAxes = axes('Parent',hFigure,'XLim',[-0.5 N-0.5],...
               'YLim',[min(yPoints)-0.5 max(yPoints)+0.5],...
               'YColor','w','XTick',0:N-1,'LineWidth',2);
  hold on;
  plot(hAxes,xLines,yLines,'k','LineWidth',2);
  plot(hAxes,xPoints,yPoints,'o','MarkerFaceColor',[0.96 0.96 0.86],...
       'MarkerSize',30,'MarkerEdgeColor','k','LineWidth',2);
  text(xPoints,yPoints,values,'Parent',hAxes,...
       'HorizontalAlignment','center');
  hold off;

end

这是一个样本矩阵测试:

>> A = triu(reshape(1:25,5,5))

A =

     1     6    11    16    21
     0     7    12    17    22
     0     0    13    18    23
     0     0     0    19    24
     0     0     0     0    25

>> plot_lattice(A);

enter image description here

关于matlab - 如何在Matlab中画出这种树状的点阵结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5208501/

相关文章:

linux - 程序启动后 GC 开销限制超出错误 - 没有运行脚本

matlab - 创建向量

matlab - (Matlab)如何修改只读光流对象属性

java - 从通用树到领域特定树

c++ - C++的2D分段/四元树解释

python - 如何使用 matlab 引擎 API 从 python 调用 matlab 函数文件?

python - 如何使用 python 以一种自然的人类可读的方式绘制与时间相关的信息?

r - ggplot 绘图中的字体大小与使用 knitr 的绘图有关

plot - 如何从 Plotly 中删除轴和数字

mysql - SELECT a number 是什么意思?