MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   如何在Matlab中绘制这种像格子结构的树? (https://www.labfans.com/bbs/showthread.php?t=26501)

poster 2019-12-14 20:13

如何在Matlab中绘制这种像格子结构的树?
 
我要绘制的结构应与所示结构完全相同。谢谢! [IMG]https://i.stack.imgur.com/pyimb.jpg[/IMG]



[B]回答:[/B]

这是我整理的一个小功能,它将一个正方形的上三角矩阵作为输入,并按上述方式绘制晶格结构:

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); [IMG]https://i.stack.imgur.com/RSYZ7.png[/IMG]



[url=https://stackoverflow.com/questions/5208501]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 04:51

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.