Graph Plotting and Customization

This example shows how to plot graphs, and then customize the display to add labels or highlighting to the graph nodes and edges.

Graph Plotting Objects

Use the plot function to plot graph and digraph objects. By default, plot examines the size and type of graph to determine which layout to use. The resulting figure window contains no axes tick marks. However, if you specify the (x,y) coordinates of the nodes with the XData , YData , or ZData name-value pairs, then the figure includes axes ticks.

Node labels are included automatically in plots of graphs that have 100 or fewer nodes. The node labels use the node names if available; otherwise, the labels are numeric node indices.

For example, create a graph using the buckyball adjacency matrix, and then plot the graph using all of the default options. If you call plot and specify an output argument, then the function returns a handle to a GraphPlot object. Subsequently, you can use this object to adjust properties of the plot. For example, you can change the color or style of the edges, the size and color of the nodes, and so on.

G = graph(bucky); p = plot(G)

p = GraphPlot with properties: NodeColor: [0 0.4470 0.7410] MarkerSize: 4 Marker: 'o' EdgeColor: [0 0.4470 0.7410] LineWidth: 0.5000 LineStyle: '-' NodeLabel: EdgeLabel: <> XData: [0.1033 1.3374 2.2460 1.3509 0.0019 -1.0591 -2.2901 -2.8275 -1.9881 -0.8836 1.5240 0.4128 0.6749 1.9866 2.5705 3.3263 3.5310 3.9022 3.8191 3.5570 1.5481 2.6091 1.7355 0.4849 0.2159 -1.3293 -1.2235 -2.3934 -3.3302 . ] (1x60 double) YData: [-1.8039 -1.2709 -2.0484 -3.0776 -2.9916 -0.9642 -1.2170 0.0739 1.0849 0.3856 0.1564 0.9579 2.2450 2.1623 0.8879 -1.2600 0.0757 0.8580 -0.4702 -1.8545 -3.7775 -2.9634 -2.4820 -3.0334 -3.9854 -3.2572 -3.8936 -3.1331 . ] (1x60 double) ZData: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] Use GET to show all properties

After you have a handle to the GraphPlot object, use dot indexing to access or change the property values. For a complete list of the properties that you can adjust, see GraphPlot Properties.

Change the value of NodeColor to 'red' .

p.NodeColor = 'red';