In this section, we will cover the fundamentals of plotting in MATLAB. Plotting is a crucial skill for visualizing data and results, and MATLAB provides a powerful set of tools for creating a wide variety of plots. By the end of this section, you will be able to create basic plots and understand the essential functions and commands used in MATLAB for plotting.

Key Concepts

  1. Introduction to Plotting
  2. Basic Plotting Functions
  3. Customizing Plots
  4. Saving and Exporting Plots

  1. Introduction to Plotting

Plotting in MATLAB is straightforward and involves using built-in functions to create visual representations of data. The most common function for creating 2D plots is plot.

Example: Simple Line Plot

% Define data
x = 0:0.1:10; % x values from 0 to 10 with a step of 0.1
y = sin(x);   % y values as the sine of x

% Create a plot
figure;       % Open a new figure window
plot(x, y);   % Plot y versus x
title('Simple Line Plot'); % Add a title
xlabel('x');  % Label x-axis
ylabel('sin(x)'); % Label y-axis

Explanation

  • x = 0:0.1:10; creates a vector x with values from 0 to 10 in steps of 0.1.
  • y = sin(x); computes the sine of each element in x.
  • figure; opens a new figure window.
  • plot(x, y); creates a 2D line plot of y versus x.
  • title, xlabel, and ylabel add a title and labels to the axes.

  1. Basic Plotting Functions

2.1 plot

The plot function is used for creating 2D line plots.

% Example: Multiple lines in one plot
x = 0:0.1:10;
y1 = sin(x);
y2 = cos(x);

figure;
plot(x, y1, '-r', x, y2, '--b'); % '-r' for red solid line, '--b' for blue dashed line
title('Sine and Cosine Functions');
xlabel('x');
ylabel('y');
legend('sin(x)', 'cos(x)'); % Add a legend

2.2 scatter

The scatter function creates scatter plots.

% Example: Scatter plot
x = randn(1, 100); % 100 random numbers from a normal distribution
y = randn(1, 100);

figure;
scatter(x, y, 'filled'); % 'filled' for filled circles
title('Scatter Plot');
xlabel('x');
ylabel('y');

2.3 bar

The bar function creates bar charts.

% Example: Bar chart
categories = {'A', 'B', 'C', 'D'};
values = [4, 7, 1, 8];

figure;
bar(values);
set(gca, 'XTickLabel', categories); % Set x-axis labels
title('Bar Chart');
xlabel('Category');
ylabel('Value');

  1. Customizing Plots

MATLAB provides various options to customize plots, including changing colors, line styles, markers, and adding annotations.

Example: Customizing a Plot

x = 0:0.1:10;
y = sin(x);

figure;
plot(x, y, 'LineWidth', 2, 'Color', [0.2, 0.6, 0.8]); % Custom line width and color
title('Customized Plot');
xlabel('x');
ylabel('sin(x)');
grid on; % Add grid lines

Explanation

  • 'LineWidth', 2 sets the line width to 2.
  • 'Color', [0.2, 0.6, 0.8] sets the line color using RGB values.
  • grid on adds grid lines to the plot.

  1. Saving and Exporting Plots

You can save and export plots in various formats using the saveas and print functions.

Example: Saving a Plot

x = 0:0.1:10;
y = sin(x);

figure;
plot(x, y);
title('Plot to Save');

% Save the plot as a PNG file
saveas(gcf, 'sine_plot.png');

% Save the plot as a PDF file
print('sine_plot', '-dpdf');

Explanation

  • saveas(gcf, 'sine_plot.png'); saves the current figure (gcf) as a PNG file.
  • print('sine_plot', '-dpdf'); saves the plot as a PDF file.

Practical Exercises

Exercise 1: Create a Line Plot

Task: Create a line plot of the function \( y = e^{-x} \cos(2\pi x) \) for \( x \) values from 0 to 5.

Solution:

x = 0:0.1:5;
y = exp(-x) .* cos(2 * pi * x);

figure;
plot(x, y);
title('Exponential Decay with Cosine');
xlabel('x');
ylabel('y');

Exercise 2: Create a Bar Chart

Task: Create a bar chart for the following data: Categories = {'Red', 'Green', 'Blue', 'Yellow'}, Values = [5, 3, 9, 6].

Solution:

categories = {'Red', 'Green', 'Blue', 'Yellow'};
values = [5, 3, 9, 6];

figure;
bar(values);
set(gca, 'XTickLabel', categories);
title('Color Values');
xlabel('Color');
ylabel('Value');

Exercise 3: Create a Scatter Plot

Task: Create a scatter plot of 50 random points with x and y values between 0 and 1.

Solution:

x = rand(1, 50);
y = rand(1, 50);

figure;
scatter(x, y, 'filled');
title('Random Scatter Plot');
xlabel('x');
ylabel('y');

Common Mistakes and Tips

  • Mistake: Forgetting to use figure to open a new figure window, which can overwrite existing plots.
    • Tip: Always use figure before creating a new plot to avoid overwriting.
  • Mistake: Not labeling axes or adding titles, which can make plots hard to interpret.
    • Tip: Always use title, xlabel, and ylabel to make your plots informative.

Conclusion

In this section, we covered the basics of plotting in MATLAB, including creating simple line plots, scatter plots, and bar charts. We also learned how to customize plots and save them in different formats. These skills are fundamental for visualizing data and results in MATLAB. In the next section, we will delve deeper into 2D plotting techniques.

© Copyright 2024. All rights reserved