2D and 3D plots, subplots, colormaps, axes customization, and exporting publication-quality figures for papers and reports.
2D and 3D plots, subplots, colormaps, axes customization, and exporting publication-quality figures for papers and reports.
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]);
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');
% 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
% 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');
exportgraphics over saveas. exportgraphics (MATLAB R2020a+) produces cleaner output with proper DPI settings. For vector PDFs in LaTeX, use 'ContentType','vector' to avoid rasterization.T = 15 + 12*sin(2*pi*(1:120)/12) + randn(5,120)*2. Each row is a city.colororder to set a custom 5-color palette.imagesc showing the 5×120 temperature matrix. Add a colorbar and proper axis labels (month numbers on x, city names on y).hold on/off to layer multiple plots on the same axes.subplot(r,c,i) for static grids; tiledlayout for flexible spacing.surf + shading interp + colormap for smooth 3D surfaces.set(gca, ...) for font, ticks, and grid.exportgraphics for high-res PNG; vector PDF for LaTeX.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.
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.
Before moving on, verify you can answer these without looking:
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 →