Skip to content

MATLAB - Scatter Plots

MATLAB - Modern Scatter Plots for Data Visualization

Section titled “MATLAB - Modern Scatter Plots for Data Visualization”

A scatter plot is an essential tool for exploratory data analysis (EDA). It visualizes the relationship between two numerical variables by displaying individual data points. Scatter plots are excellent for identifying trends, correlations, clusters, and outliers in your data. MATLAB’s scatter function is a powerful and flexible tool for creating these plots.

The core function for creating scatter plots is scatter. While it has many syntaxes, the most modern and readable approach uses name-value pairs for customization.

scatter(x, y, ...)

Where x and y are vectors of the same length containing the coordinates of the data points. Additional properties can be specified as name-value pairs.

Let’s explore customization using a simple dataset.

%% Generate sample data
x = linspace(0, 3*pi, 100);
y = sin(x) + 0.2*randn(1, 100);
figure; % Create a new figure window
scatter(x, y);
title('Basic Scatter Plot');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

Example 2: Customizing Marker Size, Color, and Style

Section titled “Example 2: Customizing Marker Size, Color, and Style”

You can control the appearance of each marker individually. Let’s vary the marker size and color based on other data vectors.

%% Create additional data for size and color
marker_sizes = randi([10, 80], 1, 100); % Vector of sizes for each point
marker_colors = x; % Color points based on their x-value
figure;
scatter(x, y, marker_sizes, marker_colors, ...
'Marker', 'o', ... % Use circle markers
'MarkerFaceAlpha', 0.6, ... % Make markers semi-transparent
'MarkerEdgeColor', 'k', ... % Black edge for markers
'LineWidth', 1.0); % Thicker edge
title('Customized Scatter Plot');
xlabel('X-Value');
ylabel('Y-Value');
cb = colorbar; % Add a color bar to show the color scale
ylabel(cb, 'Color Mapped to X-Value'); % Label the color bar
grid on;

Mini-Project: Visualizing the Fisher’s Iris Dataset

Section titled “Mini-Project: Visualizing the Fisher’s Iris Dataset”

A common use for scatter plots is to visualize grouped data. Let’s use the built-in Fisher’s Iris dataset to plot sepal length vs. sepal width, colored by species. This is a classic example of classification data visualization.

%% Load the dataset
% This creates 'meas' (measurements) and 'species' (cell array of names)
load fisheriris;
% Extract the relevant data columns
x_data = meas(:, 1); % Sepal Length
y_data = meas(:, 2); % Sepal Width
%% Modern Grouped Scatter Plot using gscatter
% gscatter is specifically designed for this task and automatically handles
% colors, markers, and the legend.
figure;
gscatter(x_data, y_data, species, 'rgb', 'osd', [], 'on');
title('Fisher''s Iris Dataset: Sepal Dimensions');
xlabel('Sepal Length (cm)');
ylabel('Sepal Width (cm)');
legend('Location', 'best');
grid on;

When creating plots for reports or presentations, consider the following:

  • Always Label Your Axes: A plot without labels is meaningless. Use units where appropriate.
  • Provide a Title: Give your plot a descriptive title.
  • Use a Legend for Grouped Data: Ensure every group is clearly identified.
  • Accessibility (a11y): Don’t rely on color alone to distinguish between groups. Use different marker shapes (e.g., 'o', 's', 'd', '^') as gscatter does. This ensures the plot is readable by people with color vision deficiencies and in black-and-white prints.
  • Clarity: Use transparency (MarkerFaceAlpha) or smaller markers if data points are densely packed to avoid overplotting.

MATLAB’s modern graphics system allows for extensive customization. You can click on elements in the plot window to open a property editor or use the set function to programmatically control every aspect of your plot’s appearance.