How to highlight with multiple colors?

13 views (last 30 days)
lewww
lewww on 27 Mar 2021
Edited: lewww on 27 Mar 2021
I would like to plot a graph and highlight edges with multiple colors, sometimes the same edge. If I use the highlight(h, G, ...) command it seems to overwrite the previous highlight command, even if I use hold on. Below is an illustrative example, the grah is a triangle on 1, 2, 3 and I wish to highlight edge 12 and 23 with 10 different colors in the hue scale, each with decreasing width. I added a pause command so the steps are visible and bunch of hold on's to see if any of them gives the desired result. But unfortunately the new highligh always overwrites the previous. I've tried additionally to create multiple edges but the highlight command colors all of them.
G = graph([1, 2, 1], [2, 3, 3]);
H = graph([1, 2],[2, 3]);
h = plot(G);
hold on
for I=1:10
hold on
highlight(h, H, 'EdgeColor', hsv2rgb([I/10, 1, 1]), 'LineWidth', 11-I);
hold on
pause(0.5);
end
hold off

Accepted Answer

lewww
lewww on 27 Mar 2021
I've solved it by plotting the same graph multiple times on the same plot and highlighting on each new plot the edges. Here's the solution for the example. It is kind of annoying to plot the same graph multiple times. It would be great to have the highlight function to work without overriding. Or an alternative solution would be that highlight returns the plot and further modifications can be called on the new plot.
G = graph([1, 2, 1], [2, 3, 3]);
H = graph([1, 2],[2, 3]);
for I=1:10
h = plot(G);
highlight(h, H, 'EdgeColor', hsv2rgb([I/10, 1, 1]), 'LineWidth', 11-I);
hold on
end
hold off
  2 Comments
Walter Roberson
Walter Roberson on 27 Mar 2021
The example https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.graphplot.highlight.html#buzh548 implies redrawing is not necessary; it makes two highlight() calls that do not appear to clash.
lewww
lewww on 27 Mar 2021
Edited: lewww on 27 Mar 2021
But in that example the second highlight only applies to nodes. The difference between node and edge highlighting seems to be the requirement that edge highlight requires the node set to be the same, meanwhile the node highlighting can work independently.

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!