Overlaying Plots in MATLAB

Learn how to effectively use and combine multiple plots using MATLAB’s plot overlay features, providing you a better understanding of your data through layered visual representations. …

Updated October 18, 2023


Hey! If you love Computer Vision and AI, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Learn how to effectively use and combine multiple plots using MATLAB’s plot overlay features, providing you a better understanding of your data through layered visual representations.

Introduction

MATLAB is a powerful environment for computational and visualization tasks. One of the most versatile tools in this software package is its ability to plot various types of graphs. This flexibility allows for easy comparison, analysis, and interpretation of your data. Sometimes, however, you may want to overlay multiple plots on a single graph or combine several visual representations.

In this article, we will guide you through the process of layering plots using MATLAB by creating customizable charts that can display different aspects of your data simultaneously. We will also demonstrate the use of various plot types and discuss how to fine-tune these overlays for greater data insights.

Overlaying Plots: A Step-by-Step Guide

  1. Import Your Data To begin, you’ll need data in the form of vectors or matrices to be plotted. You can load data from external files, like CSV, TXT, or Excel, or generate data programmatically using MATLAB functions such as rand and meshgrid. Make sure your dataset has appropriate labels for each column or row if you want a specific variable to be represented in the plot.

  2. Define Plot Types Choose which plot types you would like to use for your overlayed visualization. For instance, you can combine different MATLAB plot functions such as line plots, bar charts, scatter plots, and pie charts for a rich data representation. Keep in mind that the more complex the plot becomes, the harder it may be to interpret, so try to maintain simplicity while still capturing crucial information.

  3. Create Subplots To overlay multiple plots on a single graph, you must create subplots using MATLAB’s figure and axes commands. A figure is a container for one or more plots, while an axis represents the x-y plane for plotting. By dividing your primary figure into individual axes, you can assign different types of plots to each subplot for an overlaid view.

  4. Populate Subplots with Data Now that you have defined your plot types and set up the appropriate subplots, populate them with data using MATLAB’s plot functions such as plot, bar, or pie to represent your datasets. Make sure each function has the necessary inputs of X-data (typically row vectors or matrices), Y-data (vectors representing numerical values), and optional parameters for customizing the plot appearance, like color and markers.

  5. Adjust Plot Appearance To create a unified look across all overlapped plots, consider adjusting certain graphical properties to match. These can include line styles, colors, markers, fonts, labels, and titles. Use MATLAB’s plotting functions with additional input arguments to customize your plot’s appearance for improved readability and visual consistency.

  6. Overlay Plots Using legend In some cases, you may want to overlay multiple line plots while retaining their unique colors or markers to differentiate data points easily. To achieve this, use MATLAB’s legend command to display a separate key for each plot type, indicating the corresponding colors and marker shapes within the overlapped visualization.

  7. Finalize Plot Overlay Once you have created your subplots, populated them with data, and adjusted the graphical properties as desired, make sure to save or print your overlaid plots using MATLAB’s figure functions like savefig, print, or export_png. This will allow you to share and archive your visualization results for future reference.

Example Code Snippets

  1. Import Data from Excel File:
data = xlsread('myDataset.xlsx', 'MyData');
  1. Create Subplots with Custom Labels:
figure;
axesLabelNames = {'Line Plot', 'Bar Chart', 'Scatter Plot'};
for i = 1 : numel(axesLabelNames)
    axes(i); % Create a new axis (subplot)
    title(axesLabelNames{i}); % Set the axis label
end
  1. Populate Subplots with Data:
% Line Plot
plot(data(:,1), data(:,2), 'LineWidth', 2);

% Bar Chart
bar(data(:,3));

% Scatter Plot
scatter(data(:,4), data(:,5))
  1. Adjusting Plot Appearance:
% Line Plot Properties
set(get(gca,'Children'), 'LineWidth', 2);

% Bar Chart Properties
set(get(gca,'Children'), 'FaceColor', [1 0 0]); % Red for Bar Chart

% Scatter Plot Properties
scatter(data(:,4), data(:,5), 'MarkerSize', 6, 'MarkerFaceColor', [0.8 0.2 0.3], 'EdgeColor', 'none');
  1. Overlay Plots with legend:
legend('Line Plot', 'Bar Chart', 'Scatter Plot', 'Location', 'NorthEastOutside')
  1. Finalize and Save the Overlaid Plot:
figure; % Create a new figure window
gcf; % Get the current figure handle
print(gcf, ['myOverlaidPlots-' date('ddmmyy_hhmmss') '.pdf']); % Print the figure to PDF file

Conclusion

MATLAB offers powerful tools for creating visually appealing and insightful data representations using its plot layering capabilities. By following the steps outlined above, you can effectively merge different graph types into a cohesive visual representation of your data while optimizing the efficiency of your analysis workflows. Use these techniques to generate well-organized plots with a high level of customization for better data understanding and improved decision-making in various domains.