Main Content

GraphPlot

Graph plot for directed and undirected graphs

Description

Graph plots are the primary way to visualize graphs and networks created using the graph and digraph functions. After you create a GraphPlot object, you can modify aspects of the plot by changing its property values. This is particularly useful for modifying the display of the graph nodes or edges.

Creation

To create a GraphPlot object, specify an output argument with the plot function. For example:

G = graph([1 1 1 1 5 5 5 5],[2 3 4 5 6 7 8 9]);
h = plot(G)

Properties

GraphPlot PropertiesGraph plot appearance and behavior

Object Functions

layoutChange layout of graph plot
highlightHighlight nodes and edges in plotted graph
labelnodeLabel graph nodes
labeledgeLabel graph edges

Examples

collapse all

Create a GraphPlot object, and then show how to adjust the properties of the object to affect the output display.

Create and plot a graph.

s = [1 1 1 1 1 1 1 9 9 9 9 9 9 9];
t = [2 3 4 5 6 7 8 2 3 4 5 6 7 8];
G = graph(s,t);
h = plot(G)

Figure contains an axes object. The axes object contains an object of type graphplot.

h = 
  GraphPlot with properties:

     NodeColor: [0 0.4470 0.7410]
    MarkerSize: 4
        Marker: 'o'
     EdgeColor: [0 0.4470 0.7410]
     LineWidth: 0.5000
     LineStyle: '-'
     NodeLabel: {'1'  '2'  '3'  '4'  '5'  '6'  '7'  '8'  '9'}
     EdgeLabel: {}
         XData: [-0.0552 -0.5371 1.4267 -0.4707 -2.0048 -1.9560 2.1807 1.3586 0.0577]
         YData: [-0.3011 -2.1306 1.6662 2.1447 -0.8743 0.9689 -0.0560 -1.7169 0.2991]
         ZData: [0 0 0 0 0 0 0 0 0]

  Use GET to show all properties

Use custom node coordinates for the graph nodes.

h.XData = [0 -3 -2 -1 0 1 2 3 0];
h.YData = [2 0 0 0 0 0 0 0 -2];

Figure contains an axes object. The axes object contains an object of type graphplot.

Make the graph nodes red.

h.NodeColor = 'r';

Figure contains an axes object. The axes object contains an object of type graphplot.

Use dashed lines for the graph edges.

h.LineStyle = '--';

Figure contains an axes object. The axes object contains an object of type graphplot.

Increase the size of the nodes.

h.MarkerSize = 8;

Figure contains an axes object. The axes object contains an object of type graphplot.

Use the savefig function to save a graph plot figure.

s = [1 1 1 2 2 3 3 4 5 5 6 7];
t = [2 4 5 3 6 4 7 8 6 8 7 8];
G = graph(s,t);
plot(G);
savefig('cubegraph.fig');
clear s t G
close gcf

Use openfig to load the graph plot figure back into MATLAB®. openfig also returns a handle to the figure, y.

y = openfig('cubegraph.fig');

Figure contains an axes object. The axes object contains an object of type graphplot.

Use the findobj function to locate the correct object handle using one of the property values. Using findobj allows you to continue manipulating the original GraphPlot object used to generate the figure.

h = findobj('Marker','o')
h = 
  GraphPlot with properties:

     NodeColor: [0 0.4470 0.7410]
    MarkerSize: 4
        Marker: 'o'
     EdgeColor: [0 0.4470 0.7410]
     LineWidth: 0.5000
     LineStyle: '-'
     NodeLabel: {'1'  '2'  '3'  '4'  '5'  '6'  '7'  '8'}
     EdgeLabel: {}
         XData: [-0.0495 -0.0119 -1.5285 -1.5694 1.5285 1.5694 0.0495 0.0119]
         YData: [-2.0789 -0.5215 0.8917 -0.8184 -0.8917 0.8184 2.0789 0.5215]
         ZData: [0 0 0 0 0 0 0 0]

  Use GET to show all properties

Version History

Introduced in R2015b

expand all