In this section, we will explore how to create and customize 2D plots in MATLAB. Plotting data is a crucial aspect of data analysis and visualization, and MATLAB provides a wide range of functions to create various types of 2D plots.
Key Concepts
- Basic Plotting Functions
- Customizing Plots
- Multiple Plots in One Figure
- Annotations and Labels
- Saving Plots
- Basic Plotting Functions
plot
Function
The plot
function is the most commonly used function for creating 2D plots. It plots the data points defined by their x and y coordinates.
% Example: Basic Line Plot 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 plot(x, y); % create the plot title('Sine Wave'); % add title xlabel('x'); % label x-axis ylabel('sin(x)'); % label y-axis
scatter
Function
The scatter
function creates a scatter plot with individual points.
% Example: Scatter Plot x = rand(1, 50); % 50 random x values y = rand(1, 50); % 50 random y values scatter(x, y, 'filled'); % create the scatter plot with filled circles title('Random Scatter Plot'); xlabel('x'); ylabel('y');
- Customizing Plots
Line Styles, Markers, and Colors
You can customize the appearance of your plots by specifying line styles, markers, and colors.
% Example: Customizing Line Style, Marker, and Color x = 0:0.1:10; y1 = sin(x); y2 = cos(x); plot(x, y1, '-r', 'LineWidth', 2); % red solid line with width 2 hold on; % hold the current plot plot(x, y2, '--b', 'LineWidth', 2); % blue dashed line with width 2 hold off; % release the plot title('Sine and Cosine Waves'); xlabel('x'); ylabel('y'); legend('sin(x)', 'cos(x)'); % add legend
Axis Limits and Ticks
You can set the limits and ticks of the axes to better control the display of your data.
% Example: Setting Axis Limits and Ticks x = 0:0.1:10; y = sin(x); plot(x, y); title('Sine Wave with Custom Axis Limits'); xlabel('x'); ylabel('sin(x)'); xlim([0 12]); % set x-axis limits ylim([-1.5 1.5]); % set y-axis limits xticks(0:2:12); % set x-axis ticks yticks(-1.5:0.5:1.5); % set y-axis ticks
- Multiple Plots in One Figure
Using hold on
and hold off
You can plot multiple datasets on the same figure using hold on
and hold off
.
% Example: Multiple Plots in One Figure x = 0:0.1:10; y1 = sin(x); y2 = cos(x); plot(x, y1, '-r'); % plot first dataset hold on; % hold the current plot plot(x, y2, '--b'); % plot second dataset hold off; % release the plot title('Sine and Cosine Waves'); xlabel('x'); ylabel('y'); legend('sin(x)', 'cos(x)');
Subplots
The subplot
function allows you to create multiple plots in a single figure window.
% Example: Subplots x = 0:0.1:10; y1 = sin(x); y2 = cos(x); subplot(2, 1, 1); % create a 2x1 grid, access the first subplot plot(x, y1); title('Sine Wave'); xlabel('x'); ylabel('sin(x)'); subplot(2, 1, 2); % access the second subplot plot(x, y2); title('Cosine Wave'); xlabel('x'); ylabel('cos(x)');
- Annotations and Labels
Adding Text Annotations
You can add text annotations to your plots to highlight specific points or areas.
% Example: Adding Text Annotations x = 0:0.1:10; y = sin(x); plot(x, y); title('Sine Wave with Annotations'); xlabel('x'); ylabel('sin(x)'); text(5, 0, 'Midpoint', 'HorizontalAlignment', 'center'); % add text annotation
Adding Arrows
You can use the annotation
function to add arrows and other shapes.
% Example: Adding Arrows x = 0:0.1:10; y = sin(x); plot(x, y); title('Sine Wave with Arrow'); xlabel('x'); ylabel('sin(x)'); annotation('arrow', [0.5, 0.7], [0.5, 0.8]); % add arrow annotation
- Saving Plots
Saving as Image Files
You can save your plots as image files using the saveas
function.
% Example: Saving Plot as Image File x = 0:0.1:10; y = sin(x); plot(x, y); title('Sine Wave'); xlabel('x'); ylabel('sin(x)'); saveas(gcf, 'sine_wave.png'); % save the current figure as a PNG file
Saving as MATLAB Figures
You can also save your plots as MATLAB figure files using the savefig
function.
% Example: Saving Plot as MATLAB Figure x = 0:0.1:10; y = sin(x); plot(x, y); title('Sine Wave'); xlabel('x'); ylabel('sin(x)'); savefig('sine_wave.fig'); % save the current figure as a MATLAB figure 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 \) ranging from 0 to 5. Customize the plot with a green dashed line and add appropriate labels and title.
Solution:
x = 0:0.1:5; y = exp(-x) .* cos(2 * pi * x); plot(x, y, '--g', 'LineWidth', 2); title('Damped Cosine Wave'); xlabel('x'); ylabel('y = e^{-x} \cos(2\pi x)');
Exercise 2: Create a Scatter Plot
Task: Create a scatter plot of 100 random points with x and y values between 0 and 1. Customize the points to be red and add appropriate labels and title.
Solution:
x = rand(1, 100); y = rand(1, 100); scatter(x, y, 'r', 'filled'); title('Random Scatter Plot'); xlabel('x'); ylabel('y');
Exercise 3: Create Subplots
Task: Create a figure with two subplots. The first subplot should be a line plot of \( y = \sin(x) \) and the second subplot should be a line plot of \( y = \cos(x) \) for \( x \) ranging from 0 to 2π.
Solution:
x = 0:0.1:2*pi; y1 = sin(x); y2 = cos(x); subplot(2, 1, 1); plot(x, y1); title('Sine Wave'); xlabel('x'); ylabel('sin(x)'); subplot(2, 1, 2); plot(x, y2); title('Cosine Wave'); xlabel('x'); ylabel('cos(x)');
Conclusion
In this section, we covered the basics of creating and customizing 2D plots in MATLAB. We learned how to use the plot
and scatter
functions, customize plots with different line styles, markers, and colors, and create multiple plots in one figure using hold on
and subplot
. We also explored how to add annotations and save plots. These skills are essential for effectively visualizing and presenting data in MATLAB. In the next section, we will delve into 3D plots and explore how to visualize data in three dimensions.
MATLAB Programming Course
Module 1: Introduction to MATLAB
- Getting Started with MATLAB
- MATLAB Interface and Environment
- Basic Commands and Syntax
- Variables and Data Types
- Basic Operations and Functions
Module 2: Vectors and Matrices
- Creating Vectors and Matrices
- Matrix Operations
- Indexing and Slicing
- Matrix Functions
- Linear Algebra in MATLAB
Module 3: Programming Constructs
- Control Flow: if, else, switch
- Loops: for, while
- Functions: Definition and Scope
- Scripts vs. Functions
- Debugging and Error Handling
Module 4: Data Visualization
Module 5: Data Analysis and Statistics
- Importing and Exporting Data
- Descriptive Statistics
- Data Preprocessing
- Regression Analysis
- Statistical Tests