Graph Plotting and Customization - MATLAB & Simulink (2024)

Open Live Script

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)

Graph Plotting and Customization- MATLAB & Simulink (1)

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: {1x60 cell} 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';

Graph Plotting and Customization- MATLAB & Simulink (2)

Determine the line width of the edges.

ans = 0.5000

Create and Plot Graph

Create and plot a graph representing an L-shaped membrane constructed from a square grid with a side of 12 nodes. Specify an output argument with plot to return a handle to the GraphPlot object.

n = 12;A = delsq(numgrid('L',n));G = graph(A,'omitselfloops')
G = graph with properties: Edges: [130x2 table] Nodes: [75x0 table]
p = plot(G)

Graph Plotting and Customization- MATLAB & Simulink (3)

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: {1x75 cell} EdgeLabel: {} XData: [-2.5225 -2.1251 -1.6498 -1.1759 -0.7827 -2.5017 -2.0929 -1.6027 -1.1131 -0.7069 -2.4678 -2.0495 -1.5430 -1.0351 -0.6142 -2.4152 -1.9850 -1.4576 -0.9223 -0.4717 -2.3401 -1.8927 -1.3355 -0.7509 -0.2292 -2.2479 -1.7828 ... ] (1x75 double) YData: [-3.5040 -3.5417 -3.5684 -3.5799 -3.5791 -3.0286 -3.0574 -3.0811 -3.0940 -3.0997 -2.4191 -2.4414 -2.4623 -2.4757 -2.4811 -1.7384 -1.7570 -1.7762 -1.7860 -1.7781 -1.0225 -1.0384 -1.0553 -1.0568 -1.0144 -0.2977 -0.3097 ... ] (1x75 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] Use GET to show all properties

Change Graph Node Layout

Use the layout function to change the layout of the graph nodes in the plot. The different layout options automatically compute node coordinates for the plot. Alternatively, you can specify your own node coordinates with the XData, YData, and ZData properties of the GraphPlot object.

Instead of using the default 2-D layout method, use layout to specify the 'force3' layout, which is a 3-D force directed layout.

layout(p,'force3')view(3)

Graph Plotting and Customization- MATLAB & Simulink (4)

Proportional Node Coloring

Color the graph nodes based on their degree. In this graph, all of the interior nodes have the same maximum degree of 4, nodes along the boundary of the graph have a degree of 3, and the corner nodes have the smallest degree of 2. Store this node coloring data as the variable NodeColors in G.Nodes.

G.Nodes.NodeColors = degree(G);p.NodeCData = G.Nodes.NodeColors;colorbar

Graph Plotting and Customization- MATLAB & Simulink (5)

Edge Line Width by Weight

Add some random integer weights to the graph edges, and then plot the edges such that their line width is proportional to their weight. Since an edge line width approximately greater than 7 starts to become cumbersome, scale the line widths such that the edge with the greatest weight has a line width of 7. Store this edge width data as the variable LWidths in G.Edges.

G.Edges.Weight = randi([10 250],130,1);G.Edges.LWidths = 7*G.Edges.Weight/max(G.Edges.Weight);p.LineWidth = G.Edges.LWidths;

Graph Plotting and Customization- MATLAB & Simulink (6)

Extract Subgraph

Extract and plot the top right corner of G as a subgraph, to make it easier to read the details on the graph. The new graph, H, inherits the NodeColors and LWidths variables from G, so that recreating the previous plot customizations is straightforward. However, the nodes in H are renumbered to account for the new number of nodes in the graph.

H = subgraph(G,[1:31 36:41]);p1 = plot(H,'NodeCData',H.Nodes.NodeColors,'LineWidth',H.Edges.LWidths);colorbar

Graph Plotting and Customization- MATLAB & Simulink (7)

Label Nodes and Edges

Use labeledge to label the edges whose width is larger than 6 with the label, 'Large'. The labelnode function works in a similar manner for labeling nodes.

labeledge(p1,find(H.Edges.LWidths > 6),'Large')

Graph Plotting and Customization- MATLAB & Simulink (8)

Highlight Shortest Path

Find the shortest path between node 11 and node 37 in the subgraph, H. Highlight the edges along this path in red, and increase the size of the end nodes on the path.

path = shortestpath(H,11,37)
path = 1×10 11 12 17 18 19 24 25 30 36 37
highlight(p1,[11 37])highlight(p1,path,'EdgeColor','r')

Graph Plotting and Customization- MATLAB & Simulink (9)

Remove the node labels and colorbar, and make all of the nodes black.

p1.NodeLabel = {};colorbar offp1.NodeColor = 'black';

Graph Plotting and Customization- MATLAB & Simulink (10)

Find a different shortest path that ignores the edge weights. Highlight this path in green.

path2 = shortestpath(H,11,37,'Method','unweighted')
path2 = 1×10 11 12 13 14 15 20 25 30 31 37
highlight(p1,path2,'EdgeColor','g')

Graph Plotting and Customization- MATLAB & Simulink (11)

Plotting Large Graphs

It is common to create graphs that have hundreds of thousands, or even millions, of nodes and/or edges. For this reason, plot treats large graphs slightly differently to maintain readability and performance. The plot function makes these adjustments when working with graphs that have more than 100 nodes:

  1. The default graph layout method is always 'subspace'.

  2. The nodes are no longer labeled automatically.

  3. The MarkerSize property is set to 2. (Smaller graphs have a marker size of 4).

  4. The ArrowSize property of directed graphs is set to 4. (Smaller directed graphs use an arrow size of 7).

See Also

graph | digraph | plot | GraphPlot

Related Topics

  • Directed and Undirected Graphs
  • GraphPlot Properties
  • Add Node Properties to Graph Plot Data Tips
Graph Plotting and Customization
- MATLAB & Simulink (2024)

FAQs

How to plot a graph in MATLAB Simulink? ›

From the Simulink Editor, right-click the logging badge for a signal, and select Properties to open the Instrumentation Properties for the signal. Then, click the graphic representation of the Line. From the Instrumentation Properties, you can also select subplots where you want to plot the signal.

How to customize a graph in MATLAB? ›

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.

How to change plot appearance in MATLAB? ›

You can use the linespec argument to specify a named color, but to specify a custom color, set an object property. For example, Line objects have a Color property. Create a plot with a purple line that has circular markers. Specify only the line and marker symbols in the linespec argument.

How to edit Simulink plot? ›

To change the properties of a plot, first click the Send to Figure button in the toolstrip to open a new figure window and then use any of the buttons in the plot editing toolbar. Analysis Parameters contains parameter settings applicable to the displayed analysis.

How to plot xy graph in MATLAB simulink? ›

To plot the data on an XY plot, add the visualization to the layout. Click Visualizations and layouts, then click or click and drag the XY icon onto the subplot. To plot the signals on the XY plot, in the signal table, select the signals you want to plot. Then, assign the signal pairs to the x- and y-axes.

How to plot graphs using MATLAB? ›

How to Plot a Graph in MATLAB
  1. Step 1: Specify the Range of Values for the x variable for which you're plotting the function. This will define x. ...
  2. Step 2: Define the function, y = f(x) Declare the desired function with the “=” operator. ...
  3. Step 3: Use the plot function to generate a figure.
Jun 22, 2022

What is custom plot function in MATLAB? ›

Custom Plot Functions
  • A structure that holds the name of the variable and the function that uses it. Use this information to: ...
  • A cell array to hold the logged floating-point values for the variable. ...
  • A cell array to hold the logged values for the variable after fixed-point conversion.

How do you make a graph look good in MATLAB? ›

Direct link to this comment
  1. Increase the linewidth (2 or 3 is good).
  2. Add a grid.
  3. Add minor ticks to the axes.
  4. Plot as an area with solid line and semi-transparent fill.
  5. Set the axes limits appropriately.
  6. Add a legend.
  7. Change the font and fontsize to match the output size.
  8. Set the figure aspect ratio correctly.
Feb 27, 2016

How do I customize my MATLAB layout? ›

MATLAB provides a set of preconfigured desktop layouts that are optimized for certain workflows. To select a preconfigured layout, on the Home tab, in the Environment section, click Layout and select a layout. To restore the MATLAB desktop to its default layout, select Default.

How do you make a plot pretty in MATLAB? ›

you'll want to use the 'Color' option in your plot statement. Right after color, include a vector with the RGB color values of whatever color you want. For example plot(x,y,'Color',1/255*[148 0 211]) will produce a very nice shade of purple. You can easily find RGB color codes for various colors online.

How to change plot in MATLAB? ›

Plot edit mode allows you to use a graphical interface to edit and annotate plots easily. In plot edit mode, you can label axes, change line styles, and add text, line, and arrow annotations. If no current figure exists, plotedit will create one. plotedit( state ) changes the state of plot edit mode on the figure.

How do you customize appearance in MATLAB? ›

To change the selected theme, on the Home tab, in the Environment section, click Preferences. Select MATLAB > Appearance and select a theme from the Theme field.

How to plot a graph in Simulink? ›

you can find the "To Workspace" in simulink library. Just connect to the graph which you wan to plot. After that go to command window and type Plot( file name of To workspace) and enter you will find the graph .

How do you edit a graph in Matlab? ›

  1. To add a title, go to the Figure tab, and select Title. ...
  2. To add axes labels, go to the Figure tab, and select X-Label. ...
  3. To add a legend, go to the Figure tab, and select Legend. ...
  4. To add grid lines, go to the Figure tab, and select Grid. ...
  5. To update the code, in the selected figure, click the Update Code button.

Which command is used to plot a graph in MATLAB? ›

plot( X , Y , LineSpec ) creates the plot using the specified line style, marker, and color.

How to plot simulation results in MATLAB? ›

You can use the plot function to plot the simulation results in the Simulation Data Inspector. Simulate the model. The model logs data using the Dataset format, so all the logged data streams to the Simulation Data Inspector during simulation. mdl = "ex_vdp_simout_plot"; open_system(mdl) out = sim(mdl);

How to plot Simulink to workspace in MATLAB? ›

Plot from Simulink
  1. Add “To workspace” block into the simulation. [ ...
  2. Change the variable name according to your specification. ...
  3. Add “Clock” block into the simulation to record the time. [ ...
  4. Copy the “To workspace” block to the signal you want to plot. ...
  5. Run the simulation. ...
  6. Open a new script in MATLAB.
  7. Copy this code:
Nov 27, 2019

Top Articles
Latest Posts
Article information

Author: Jeremiah Abshire

Last Updated:

Views: 5596

Rating: 4.3 / 5 (74 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Jeremiah Abshire

Birthday: 1993-09-14

Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

Phone: +8096210939894

Job: Lead Healthcare Manager

Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.