Plot graph with different markers

134 ビュー (過去 30 日間)
Rub Ron
Rub Ron 2020 年 2 月 9 日
編集済み: dpb 2023 年 7 月 14 日
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
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]
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 Center および File ExchangeDiscrete Data Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by