Plot graph with different markers
79 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
dpb
on 9 Feb 2020
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 Comments
KHAN MUBASHER AHMED
on 14 Jul 2023
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]
hG=plot(G,'Marker',mkrs(Gtype));
dpb
on 14 Jul 2023
Edited: dpb
on 14 Jul 2023
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.
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!