Plot graph with different markers

81 次查看(过去 30 天)
Hello, I have a graph G lets say with 100 egdes and 40 nodes. The G.nodes are of 3 types stored in a vector type (40x1). How can I plot the graph so that each node adopt the mark corresponding to its type?
h = plot(G,'NodeLabel',mylabel);
mylabel is a 0x1 vector with the labelling of the nodes.
Any references would be really appreciatted.

采纳的回答

dpb
dpb 2020-2-9
See <Marker in Graphplot properties>. Pass an index array to the position of the desired marker type as the 'Marker' property.
mkrs=['o';'x','+']; % the desired markers lookup table
Gtype=[....]; % vector of 1,2,3 defining which marker for each element of G
hG=plot(G,'Marker',mkrs(Gtype));
Property 'NodeLabel' could be used to write some label instead of using the plot marker by the same lookup logic if your desired labels are something different than available plot markers.
  3 个评论
KHAN MUBASHER AHMED
I have tried running the code given above but I cannot seem to get it to run.
I get an errir of invalid enum value. Can someone please help?
mkrs = {'o','x','+'};
Gtype=[1 2 3];
G = [2,4,6]
G = 1×3
2 4 6
hG=plot(G,'Marker',mkrs(Gtype));
Error using plot
Invalid enum value. Use one of these values: '+' | 'o' | '*' | '.' | 'x' | 'square' | 'diamond' | 'v' | '^' | '>' | '<' | 'pentagram' | 'hexagram' | '|' | '_' | 'none'.
dpb
dpb 2023-7-14
编辑:dpb 2023-7-14
plot is a single line object for each vector; hence you can't put more than one marker on a given line; the syntax above lets you select which one of the array markers to use on a given line/call, Gtype is a specific index into the array, not a vector. And, btw, the char() array shown will also work since each element is a single character, it doesn't require any fixup--although a cellstr may be preferable in many uses, it isn't necessary here, although it (or the newer yet string array) can be used here.
To put multiple markers on a given line, draw the line first, then use scatter for the markers; there's no combination of one line with multiple markers as a single object available. Of course, this sequence requires hold on to put the second graphics object on the same axes.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by