In this section, we will explore advanced plotting techniques in MATLAB. These techniques will help you create more sophisticated and informative visualizations. We will cover:

  1. Subplots and Multiple Plots
  2. Annotations and Text
  3. Customizing Axes and Legends
  4. Interactive Plots
  5. Exporting Plots

  1. Subplots and Multiple Plots

Subplots

Subplots allow you to display multiple plots in a single figure. This is useful for comparing different datasets or visualizing different aspects of the same data.

% Create a 2x2 grid of subplots
figure;
subplot(2, 2, 1); % First subplot
plot(rand(10, 1));
title('Random Data 1');

subplot(2, 2, 2); % Second subplot
plot(rand(10, 1));
title('Random Data 2');

subplot(2, 2, 3); % Third subplot
plot(rand(10, 1));
title('Random Data 3');

subplot(2, 2, 4); % Fourth subplot
plot(rand(10, 1));
title('Random Data 4');

Multiple Plots in One Figure

You can also overlay multiple plots in a single figure using the hold on command.

x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);

figure;
plot(x, y1, 'r'); % Plot sine wave in red
hold on;
plot(x, y2, 'b'); % Plot cosine wave in blue
hold off;
title('Sine and Cosine Waves');
legend('sin(x)', 'cos(x)');

  1. Annotations and Text

Annotations and text can be added to plots to provide additional information.

Adding Text

x = linspace(0, 2*pi, 100);
y = sin(x);

figure;
plot(x, y);
title('Sine Wave');
xlabel('x');
ylabel('sin(x)');

% Add text annotation
text(pi, 0, '\leftarrow \pi', 'HorizontalAlignment', 'left');

Adding Annotations

x = linspace(0, 2*pi, 100);
y = sin(x);

figure;
plot(x, y);
title('Sine Wave');
xlabel('x');
ylabel('sin(x)');

% Add annotation
annotation('textarrow', [0.3, 0.5], [0.5, 0.7], 'String', 'Peak');

  1. Customizing Axes and Legends

Customizing Axes

You can customize the appearance of axes using various properties.

x = linspace(0, 2*pi, 100);
y = sin(x);

figure;
plot(x, y);
title('Sine Wave');
xlabel('x');
ylabel('sin(x)');

% Customize axes
ax = gca;
ax.XColor = 'red';
ax.YColor = 'blue';
ax.FontSize = 12;
ax.XGrid = 'on';
ax.YGrid = 'on';

Customizing Legends

Legends can be customized to improve the readability of plots.

x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);

figure;
plot(x, y1, 'r', x, y2, 'b');
title('Sine and Cosine Waves');
legend({'sin(x)', 'cos(x)'}, 'Location', 'southwest', 'FontSize', 12);

  1. Interactive Plots

MATLAB provides tools for creating interactive plots, which allow users to explore data dynamically.

Data Cursor

The data cursor tool allows users to click on a plot to display the coordinates of the selected point.

x = linspace(0, 2*pi, 100);
y = sin(x);

figure;
plot(x, y);
title('Sine Wave');
datacursormode on;

Zoom and Pan

Zoom and pan tools allow users to explore different parts of a plot.

x = linspace(0, 2*pi, 100);
y = sin(x);

figure;
plot(x, y);
title('Sine Wave');
zoom on; % Enable zoom
pan on; % Enable pan

  1. Exporting Plots

Exporting plots is essential for including them in reports, presentations, or publications.

Saving Plots

You can save plots in various formats using the saveas function.

x = linspace(0, 2*pi, 100);
y = sin(x);

figure;
plot(x, y);
title('Sine Wave');

% Save plot as PNG
saveas(gcf, 'sine_wave.png');

% Save plot as PDF
saveas(gcf, 'sine_wave.pdf');

Exporting with High Quality

For high-quality exports, use the print function with appropriate settings.

x = linspace(0, 2*pi, 100);
y = sin(x);

figure;
plot(x, y);
title('Sine Wave');

% Export plot with high resolution
print('sine_wave_high_res', '-dpng', '-r300'); % 300 DPI resolution

Conclusion

In this section, we covered advanced plotting techniques in MATLAB, including subplots, annotations, customizing axes and legends, interactive plots, and exporting plots. These techniques will help you create more informative and visually appealing plots. In the next module, we will delve into data analysis and statistics, where you will learn how to import, preprocess, and analyze data using MATLAB.

© Copyright 2024. All rights reserved