Plotting and Data Visualization in MATLAB.
Course Title: MATLAB Programming: Applications in Engineering, Data Science, and Simulation Section Title: Plotting and Data Visualization Topic: Create visualizations for a given dataset using different types of 2D and 3D plots.(Lab topic)
Introduction
In this lab, you will learn how to create visualizations for a given dataset using different types of 2D and 3D plots in MATLAB. You will work with sample datasets to practice creating various plots and customizing them to effectively communicate your findings.
Objectives
- Create 2D and 3D plots to visualize datasets
- Customize plot appearance, including titles, labels, legends, and annotations
- Use different plot types, such as line plots, scatter plots, bar graphs, histograms, mesh, surface, and contour plots
- Practice working with sample datasets to create meaningful visualizations
Creating 2D Plots
MATLAB provides various 2D plot types to suit your visualization needs. You can create:
- Line plots:
plot(x, y)
creates a line plot of y vs. x. - Scatter plots:
scatter(x, y)
creates a scatter plot of y vs. x. - Bar graphs:
bar(x, y)
creates a bar graph of y vs. x. - Histograms:
histogram(x)
creates a histogram of x.
Customizing 2D Plots
You can customize your 2D plots by:
- Adding titles:
title('Plot Title')
- Adding labels:
xlabel('X-axis Label')
,ylabel('Y-axis Label')
- Adding legends:
legend('Plot 1', 'Plot 2')
- Adding annotations:
annotation('textarrow', [x1, x2], [y1, y2], 'String', 'Annotation')
Creating 3D Plots
MATLAB also provides various 3D plot types to suit your visualization needs. You can create:
- Mesh plots:
mesh(x, y, z)
creates a mesh plot of z vs. x and y. - Surface plots:
surf(x, y, z)
creates a surface plot of z vs. x and y. - Contour plots:
contour(x, y, z)
creates a contour plot of z vs. x and y.
Customizing 3D Plots
You can customize your 3D plots by:
- Adding titles:
title('Plot Title')
- Adding labels:
xlabel('X-axis Label')
,ylabel('Y-axis Label')
,zlabel('Z-axis Label')
- Adding legends:
legend('Plot 1', 'Plot 2')
- Adding annotations:
annotation('textarrow', [x1, x2], [y1, y2], [z1, z2], 'String', 'Annotation')
Example
Create a dataset with x, y, and z values:
x = 0:0.1:10;
y = 0:0.1:10;
z = sin(x).*cos(y);
Create a 3D surface plot:
surf(x, y, z);
title('3D Surface Plot');
xlabel('X-axis');
ylabel('Y-axis');
zlabel('Z-axis');
Customize the plot by adding a legend and annotation:
legend('Surface Plot');
annotation('textarrow', [0, 10], [0, 0], [0, 0], 'String', 'Sin(x)*Cos(y)');
Practice
Work with the following datasets to create meaningful visualizations:
- Temperature data: Create a 2D line plot to show the temperature variations over time.
time = 0:0.1:24; temperature = 20*sin(pi*time/12) + 20;
- Stock prices: Create a 2D scatter plot to show the relationship between stock prices and trading volume.
price = 100 + 10*randn(1, 100); volume = 1000 + 100*randn(1, 100);
Weather data: Create a 3D surface plot to show the relationship between temperature, humidity, and wind speed.
x = 0:0.1:10; y = 0:0.1:10; z = 20*sin(x).*cos(y) + 10*randn(size(x));
Tips and Resources
For more information on plotting in MATLAB, visit the MathWorks documentation.
- Experiment with different plot types and customization options to find the most effective way to communicate your findings.
- Use the
help
command in MATLAB to access help documentation for specific functions and syntax.
Leave a Comment or Ask for Help
Do you have questions about creating visualizations in MATLAB? Leave a comment below or ask for help. We're here to support you.
Next topic: Reading and writing data to/from files (text, CSV, Excel).
Images

Comments