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.
댓글 수: 0
채택된 답변
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
2023년 7월 14일
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
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 Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!