Data Visualization with MATLAB

Learn the fundamentals of data visualization in MATLAB - from basic plots to advanced graphical representations.

About This Tutorial

This tutorial focuses on plotting graphs for data visualization in MATLAB. You will learn how to use MATLAB's plotting functions to create line plots, scatter plots, bar charts, and more. The tutorial covers customizing graph appearance, adding labels, legends, and exporting your visualizations. By the end, you'll be able to effectively present your data using MATLAB's powerful visualization tools.

Complete Guide: 12 MATLAB Plot Types with Code & Examples

1. 2D Line Plot: plot(x, y)

The standard function for continuous 2D data visualization. Use line properties to customize colors, line width, and marker styles.

x = 0:0.1:2*pi;
y = sin(x);
plot(x, y, '-r*', 'LineWidth', 2, 'MarkerSize', 6);
xlabel('Angle (radians)');
ylabel('Amplitude');
title('Sine Wave Signal');
grid on;

2. Grid of Multiple Subplots: subplot(m, n, p)

Divide the figure window into an m-by-n matrix of smaller axes.

subplot(2,1,1);
plot(x, sin(x), 'b'); title('Sine'); grid on;

subplot(2,1,2);
plot(x, cos(x), 'r'); title('Cosine'); grid on;

3. Overlay Multiple Curves: hold on / hold off

x = 0:0.1:2*pi;
plot(x, sin(x), 'r-'); hold on;
plot(x, cos(x), 'b--'); hold off;
legend('sin(x)', 'cos(x)');
title('Sine & Cosine Comparison');

4. Scatter Plot: scatter(x, y, sz, c)

x = randn(100, 1);
y = randn(100, 1);
sz = 50; c = sqrt(x.^2 + y.^2);
scatter(x, y, sz, c, 'filled');
colorbar; title('2D Scatter Distribution');

5. Bar Chart: bar(y)

categories = {'Q1', 'Q2', 'Q3', 'Q4'};
sales = [150, 220, 180, 310];
bar(sales, 'FaceColor', [0.2 0.6 0.8]);
set(gca, 'XTickLabel', categories);
ylabel('Sales ($k)'); title('Quarterly Sales');

6. Discrete Stem Plot: stem(n, x)

n = 0:20;
x = (0.8).^n;
stem(n, x, 'filled', 'MarkerFaceColor', 'red');
xlabel('Sample Index n'); ylabel('x[n]');
title('Discrete Exponential Signal');

7. 3D Surface Plot: surf(X, Y, Z)

[X, Y] = meshgrid(-2:0.1:2, -2:0.1:2);
Z = X .* exp(-X.^2 - Y.^2);
surf(X, Y, Z); colorbar;
xlabel('X'); ylabel('Y'); zlabel('Z');
title('3D Mesh Surface Plot');

8. Histogram Data Distribution: histogram(data)

data = randn(1000, 1);
histogram(data, 30, 'FaceColor', 'g');
xlabel('Value'); ylabel('Frequency');
title('Normal Distribution Histogram');

9. Pie Chart: pie(data, explode)

data = [35, 25, 20, 20];
labels = {'EE', 'ME', 'CS', 'CE'};
explode = [1 0 0 0];
pie(data, explode, labels);
title('Engineering Major Share');

10. 3D Line Helix: plot3(x, y, z)

t = 0:pi/50:10*pi;
st = sin(t); ct = cos(t);
plot3(st, ct, t, 'LineWidth', 2);
grid on; xlabel('x'); ylabel('y'); zlabel('z');
title('3D Helix Curve');

11. 2D Contour Map: contourf(X, Y, Z)

[X, Y] = meshgrid(-3:0.125:3);
Z = peaks(X, Y);
contourf(X, Y, Z, 10); colorbar;
title('Filled 2D Contour Map');

12. Exporting Figures: saveas() & exportgraphics()

fig = gcf;
% Save high-res PNG
exportgraphics(fig, 'matlab_plot_output.png', 'Resolution', 300);
% Save vector PDF
saveas(fig, 'matlab_plot_output.pdf');
Tutorial Progress

Part 1 of 9

Duration

15 Minutes

Difficulty

Beginner