MATLAB Examples

This is an example of how to create a directed graph plot in MATLAB®.

Read about the digraph function in the MATLAB documentation. This function is available in R2015b or newer.

For more examples, go to MATLAB Plot Gallery

% Check version
if verLessThan('matlab','8.6')
    error('digraph is available in R2015b or newer.')
end

% Load ride data from Boston's bike sharing program
load BikeRideData byCities

% Create a directed graph object using the digraph function
G = digraph(byCities{:,2:5},byCities.Properties.VariableNames(2:5),'OmitSelfLoops');

% Visualize the graph
figure
plot(G,'EdgeLabel',G.Edges.Weight,'layout','layered')

% Remove axes ticks
set(gca,'XTick',[],'YTick',[])

% Add title
title('Number of bike rides between cities')