MATLAB: Is there a tool to create legends for directed/undirected graphs

directed graphgraph theorylegend

I've been playing with the directed graphing abilities and finding it to work very well. I am able to create some neat graphs that contain a lot of information packed in for visual communication in particular – setting node colors, edge color, etc. I'm finding that I'm not able to create a legend for this sort of work and have been slowly building one in a clunky way via a separate plot. Is there a utility for doing this sort of thing that I missed in the documentation?

Best Answer

No, you did not miss anything. graph objects have no useful legend.
There is a getLegendGraphic() method that can be applied to the object returned by the plot() call. That gets you a hggroup whose children you can examine. However, when I change the properties of those, nothing appears to happen. The items do not appear to have properties that can go into a legend.
You do not need another plot, though. What you can do is line() a bunch of items together using nan, nan as the coordinate and appropriate color and linestyle and marker properties, and giving them DisplayName properties that you want to appear in the legend. Then legend() that group of handles. Because the coordinates are nan, the lines will not be drawn, but the information for them will appear in the legend.