Day 02 Visualization

Plotting and Data Visualization

2D and 3D plots, subplots, colormaps, axes customization, and exporting publication-quality figures for papers and reports.

~1 hour Intermediate Hands-on Precision AI Academy

Today's Objective

2D and 3D plots, subplots, colormaps, axes customization, and exporting publication-quality figures for papers and reports.

01

2D Plots

MATLAB — Basic 2D Plotting

x = linspace(0, 2*pi, 200); y1 = sin(x); y2 = cos(x); y3 = sin(2*x) .* exp(-x/4); % Basic line plot figure; plot(x, y1, 'b-', 'LineWidth', 2); % blue solid line hold on; % don't clear on next plot plot(x, y2, 'r--', 'LineWidth', 1.5); % red dashed plot(x, y3, 'g-.', 'LineWidth', 1.5); % green dash-dot hold off; % Formatting title('Trigonometric Functions', 'FontSize', 14); xlabel('x (radians)'); ylabel('Amplitude'); legend({'sin(x)', 'cos(x)', 'sin(2x)e^{-x/4}'}, 'Location', 'best'); grid on; xlim([0, 2*pi]); ylim([-1.2, 1.2]); set(gca, 'XTick', 0:pi/2:2*pi, 'XTickLabel', {'0','π/2','π','3π/2','2π'}); % Other plot types figure; scatter(randn(100,1), randn(100,1), 50, 'filled'); title('Scatter Plot'); figure; bar([1,3,2,5,4,6], 'FaceColor', [0.2,0.4,0.8]); figure; histogram(randn(1000,1), 30, 'FaceColor', [0.8,0.3,0.2]);

02

Subplots and Tiled Layouts

MATLAB — Subplots
x = linspace(-3, 3, 300);

figure('Position', [100, 100, 1000, 600]);

% subplot(rows, cols, index)
subplot(2, 3, 1);
plot(x, x.^2);
title('y = x^2'); grid on;

subplot(2, 3, 2);
plot(x, sin(x.*pi), 'r');
title('y = sin(\pi x)'); grid on;

subplot(2, 3, 3);
plot(x, exp(-x.^2));
title('Gaussian'); grid on;

subplot(2, 3, 4);
semilogy(1:50, cumsum(rand(1,50)));
title('Log y-axis'); grid on;

subplot(2, 3, 5);
t = linspace(0, 2*pi, 100);
polarplot(t, sin(2*t).^2);
title('Polar Plot');

subplot(2, 3, 6);
[X, Y] = meshgrid(-2:0.1:2, -2:0.1:2);
Z = X .* exp(-X.^2 - Y.^2);
imagesc(Z); colorbar; colormap('jet');
title('Heat Map');

sgtitle('MATLAB Plot Gallery', 'FontSize', 16, 'FontWeight', 'bold');
03

3D Plots and Surfaces

MATLAB — 3D Visualization
% 3D surface plot
[X, Y] = meshgrid(-3:0.1:3, -3:0.1:3);
Z = sin(sqrt(X.^2 + Y.^2)) ./ (sqrt(X.^2 + Y.^2) + eps);

figure;
surf(X, Y, Z, 'EdgeColor', 'none');
colormap('parula');
colorbar;
title('sinc function');
xlabel('X'); ylabel('Y'); zlabel('Z');
shading interp;   % smooth shading

% Contour plot
figure;
contourf(X, Y, Z, 20);  % filled contours, 20 levels
colormap('coolwarm');
colorbar;
title('Contour Plot');

% 3D line plot (parametric curve)
figure;
t = linspace(0, 6*pi, 500);
plot3(sin(t), cos(t), t, 'LineWidth', 2);
grid on;
title('3D Helix');
xlabel('X'); ylabel('Y'); zlabel('Z');
view(30, 20);  % azimuth, elevation
04

Exporting Figures

MATLAB — Export for Publication
% Create a publication-quality figure
fig = figure('Units', 'inches', 'Position', [1,1,5,3.5]);

x = linspace(0, 4*pi, 500);
plot(x, sin(x), 'Color', [0.1,0.3,0.7], 'LineWidth', 1.5);
hold on;
plot(x, cos(x), 'Color', [0.8,0.3,0.1], 'LineWidth', 1.5);
hold off;

set(gca, 'FontSize', 11, 'FontName', 'Helvetica');
title('Signal Comparison', 'FontSize', 13);
xlabel('Time (s)'); ylabel('Amplitude');
legend({'sin', 'cos'}, 'FontSize', 10);
grid on; box off;

% Export as high-resolution PNG (300 DPI)
exportgraphics(fig, 'signal_plot.png', 'Resolution', 300);

% Export as vector PDF (best for LaTeX)
exportgraphics(fig, 'signal_plot.pdf', 'ContentType', 'vector');

% Export as SVG
print(fig, 'signal_plot', '-dsvg');
💡
Use exportgraphics over saveas. exportgraphics (MATLAB R2020a+) produces cleaner output with proper DPI settings. For vector PDFs in LaTeX, use 'ContentType','vector' to avoid rasterization.
Day 2 Exercise
Climate Data Dashboard
  1. Create synthetic monthly temperature data for 5 cities over 10 years: T = 15 + 12*sin(2*pi*(1:120)/12) + randn(5,120)*2. Each row is a city.
  2. Plot a line chart of all 5 cities' temperatures on one axes with a legend. Use colororder to set a custom 5-color palette.
  3. Create a heatmap using imagesc showing the 5×120 temperature matrix. Add a colorbar and proper axis labels (month numbers on x, city names on y).
  4. Add a subplot showing the annual average for each city as a bar chart with error bars (std dev).
  5. Export the final figure as a 300 DPI PNG.

Day 2 Summary

Challenge

Build an animated plot: draw a sine wave and animate a moving red dot that traces the curve in real-time using drawnow and a loop. Then export it as a GIF using imwrite with the loop option, capturing each frame with getframe.

What's Next

The foundations from today carry directly into Day 3. In the next session the focus shifts to Day 3 — building directly on everything covered here.

Day 2 Checkpoint

Before moving on, verify you can answer these without looking:

  • What is the core concept introduced in this lesson, and why does it matter?
  • What are the two or three most common mistakes practitioners make with this topic?
  • Can you explain the key code pattern from this lesson to a colleague in plain language?
  • What would break first if you skipped the safeguards or best practices described here?
  • How does today's topic connect to what comes in Day 3?

Live Bootcamp

Learn this in person — 2 days, 5 cities

Thu–Fri sessions in Denver, Los Angeles, New York, Chicago, and Dallas. $1,490 per seat. June–October 2026.

Reserve Your Seat →
Continue To Day 3
Day 3: Signal Processing