wanted: function for drawing simple network diagrams

1 次查看(过去 30 天)
We are looking for a simple way to draw diagrams with circles and lines.
Requirements: * creates a figure based on the results of a simulation (i.e. we need a function for matlab, not a separate tool like powerpoint) * a free function, not part of a paid toolbox * draws shapes (circle) and puts a label on each * places the shapes in a specific location * draws lines with arrows between the shapes * allows for the line thickness to be changed * (nice to have) allows for the colors of objects to be changed
Here is a diagram we have created and with the built in methods. We are looking for a function that does this more easily and perhaps more beautifully. http://i55.tinypic.com/1zz3z9e.jpg
We have already investigated these methods: - biograph toolbox (not free) - graphViz (might be ok, but looks complicated) - graphViz-like tools from file exchange (looks limited, i.e. arrow size cannot be changed) : http://www.mathworks.com/matlabcentral/fileexchange/27608-graphviz-like-tools-for-matlab
Thanks for any ideas!

回答(3 个)

Kelly Kearney
Kelly Kearney 2011-6-10
If you're okay with straight edges in the graphs (i.e. no need to route edges around nodes or anything like that), you can probably use gplot along with patch and text to get what you want. The gplot function is annoyingly limited in line customization and doesn't return individual handles for the lines it plots, but a series of calls to it can approximate what you want. I prefer to stay away from rectangle objects since they don't play well with axis limits, and use patch instead to generate my node objects.
Here's an example that more or less replicates your image:
% The data
x = [1 2 2 2 ]; % Node x coordinates
y = [3 3 2 1]; % Node y coordinates
lbl = cellstr(num2str((1:nx)')); % Node labels
r = 0.4; % radius of nodes
nx = length(x);
adj = zeros(nx); % Adjacency matrix for edges,
adj(1,2) = 1; % values specify line width
adj(1,3) = 2;
adj(1,4) = 2;
% The plot
figure;
axes;
hold on;
linewidth = unique(adj(adj>0));
for il = 1:length(linewidth)
h = findall(gca, 'type', 'line');
gplot(adj == linewidth(il), [x' y']);
hnew = setdiff(findall(gca, 'type', 'line'), h);
set(hnew, 'linewidth', linewidth(il));
end
theta = linspace(0, 2*pi, 20)';
xc = bsxfun(@plus, r .* cos(theta), x);
yc = bsxfun(@plus, r .* sin(theta), y);
patch(xc, yc, 'w');
text(x,y,lbl);
axis equal;
  1 个评论
Danielle Ripsman
Danielle Ripsman 2014-6-5
This was exactly what I was looking for, thanks! I did however notice an error (or well it crashed) nx = length(x); is used before it's assigned - it needs to come before line 3, rather than in line 5.

请先登录,再进行评论。


Sean de Wolski
Sean de Wolski 2011-6-10
Why not just use powerpoint/keynote/open office?
EDIT per clarification
doc rectangle %rectangles/circles etc.
doc annotation %arrows/text
doc patch %triangles
playing with these two functions should get you everything you need.
  1 个评论
Robert
Robert 2011-6-10
Thanks for your answer. We want to automatically generate this diagram based on the results of some other analysis in MATLAB. I have edited my requirements now.

请先登录,再进行评论。


Daniel Shub
Daniel Shub 2011-6-10
If the figure does not have to be manipulated in MATLAB after it has been created, I would suggest using MATLAB to write code for PGF/TikZ and then compile with LaTeX. This would give you the automation you need and image quality that is hard to obtain with straight MATLAB.

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by