2.2 - Basic plotting
Enroll to start learning
You’ve not yet enrolled in this course. Please enroll for free to listen to audio lessons, classroom podcasts and take practice test.
Interactive Audio Lesson
Listen to a student-teacher conversation explaining the topic in a relatable way.
Creating Basic Plots
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Today, we will start with how to create basic plots in MATLAB. Remember, to create a plot, we need our x and y data in the same length. If I say that `x` is a vector of points, what do I mean, Student_1?
You mean that `x` is just a list of values, right?
Exactly! Now, let's say we want to plot `x = [1, 2, 3, 4, 5, 6]` against `y = [3, -1, 2, 4, 5, 1]`. Can someone tell me the command we would use?
Is it just `plot(x,y)`?
That's correct! Make sure to always check that both vectors match in size. Let’s visualize this graph. What do you expect it to look like?
It should be some zigzagging line connecting the points!
Right on! Visualizing the points is a great way to analyze data. We’ll do more of this!
Enhancing the Plot with Labels and Titles
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
Now that we know how to create a basic plot, let's make our graphs more informative. Why do you think labels and titles are important, Student_4?
So that anyone reading the graph understands what it represents?
That's spot on! In MATLAB, we can add titles and axis labels using `xlabel`, `ylabel`, and `title`. Can anyone give me the syntax?
Like `xlabel('x-axis')` and `title('Graph Title')`?
Exactly! Using the backslash for π is also helpful. If I had a sine function plotted, how would I label it?
You'd label the y-axis as 'Sine of x'?
Great! Adding these labels makes the data interpretation easier. Let's add them now!
Multiple Data Sets in One Plot
🔒 Unlock Audio Lesson
Sign up and enroll to listen to this audio lesson
What if we have more than one dataset? Why might it be useful to display them in a single graph, Student_3?
To compare them directly, right?
Correct! We can plot multiple datasets using the `plot` function simultaneously. Can someone give me an example syntax?
Like `plot(x,y1,'--', x,y2,'-')`?
Absolutely! Each dataset can also have a different line style. How do we tell MATLAB which dataset is which?
By adding a legend!
Yes! Using the `legend` function helps clarify the data presented in the graph, making it much more useful for interpretation.
Introduction & Overview
Read summaries of the section's main ideas at different levels of detail.
Quick Overview
Standard
In this section, we explore MATLAB's powerful plotting capabilities, detailing how to create 2D plots using vectors, add titles and labels, and manage multiple datasets in a single plot. Understanding these tools enhances the analysis of mathematical functions and data visualization.
Detailed
In Markdown style, this section emphasizes the importance of plotting within MATLAB for both visualization and understanding of mathematical concepts. It starts with the fundamentals of creating 2D plots using vectors for x and y coordinates. The section explains that the primary command for plotting graphs is plot(x,y), where both x and y must be of the same size, either as row or column vectors. The guide progresses to show how to implement additional features such as axis labels and titles, explaining the syntax used, including how to incorporate the π symbol. Further, it covers how to plot multiple datasets within the same graph and customize the appearance using different line styles and colors. This comprehensive understanding assists students in effectively visualizing their data and mathematical functions.
Youtube Videos
Audio Book
Dive deep into the subject with an immersive audiobook experience.
Overview of MATLAB Plotting
Chapter 1 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
MATLAB has an excellent set of graphic tools. Plotting a given data set or the results of computation is possible with very few commands. You are highly encouraged to plot mathematical functions and results of analysis as often as possible. Trying to understand mathematical equations with graphics is an enjoyable and very efficient way of learning mathematics. Being able to plot mathematical functions and data freely is the most important step, and this section is written to assist you to do just that.
Detailed Explanation
MATLAB provides powerful tools for creating visual representations of data and mathematical functions. The goal of plotting is to help you visualize complex equations and datasets, making understanding them easier and more engaging. This section aims to empower you with the skills needed to create these plots using simple commands.
Examples & Analogies
Think of plotting like creating a picture from a recipe. Just as each ingredient adds a unique flavor to a dish, each data point adds detail to your plot, helping you to visualize complex flavors of mathematics.
Creating Simple Plots
Chapter 2 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
The basic MATLAB graphing procedure, for example in 2D, is to take a vector of x-coordinates, x = (x1,...,xN), and a vector of y-coordinates, y = (y1,...,yN), locate the points (xi,yi), with i = 1,2,...,n and then join them by straight lines. You need to prepare x and y in an identical array form; namely, x and y are both row arrays or column arrays of the same length.
The MATLAB command to plot a graph is plot(x,y). The vectors x = (1,2,3,4,5,6) and y = (3,-1,2,4,5,1) produce the picture shown in Figure 2.1.
x = [1 2 3 4 5 6];
y = [3 -1 2 4 5 1];
plot(x,y)
Detailed Explanation
To create a plot in MATLAB, you first define two sets of coordinates: x-coordinates and y-coordinates. Both these sets should be of the same length and can be either in row or column format. The command 'plot(x,y)' is used to render the graph, which connects the designated points with lines. For example, if you have arrays of x values from 1 to 6 and corresponding y values, plotting them will visualize their relationship on a 2D graph.
Examples & Analogies
Imagine mapping a journey on a map where each point you visit has coordinates (x,y). Connecting these points helps visualize your travel path, just as plotting connects data points to show trends or patterns.
Adding Titles and Labels
Chapter 3 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
MATLAB enables you to add axis labels and titles. For example, using the graph from the previous example, add an x- and y-axis labels. Now label the axes and add a title. The character \pi creates the symbol π. An example of a 2D plot is shown in Figure 2.2.
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the Sine function')
Detailed Explanation
After creating a plot, it is essential to enhance it with labels and titles to make it informative. The 'xlabel' function adds a label to the x-axis, 'ylabel' adds a label to the y-axis, and 'title' provides a title for the entire plot. This step ensures that anyone viewing the graph understands its context and content.
Examples & Analogies
Similar to how a well-labeled map enhances understanding of a geographical area, labeling your plot provides clarity. Imagine reading a book with clear headings—this helps quickly grasp the themes and subjects being discussed.
Multiple Data Sets in One Plot
Chapter 4 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
Multiple (x,y) pairs arguments create multiple graphs with a single call to plot. For example, these statements plot three related functions of x: y = 2cos(x), y = cos(x), and y = 0.5cos(x), in the interval 0 ≤ x ≤ 2π.
x = 0:pi/100:2pi;
y1 = 2cos(x);
y2 = cos(x);
y3 = 0.5cos(x);
plot(x,y1,'--',x,y2,'-',x,y3,':')
xlabel('0 ≤ x ≤ 2π')
ylabel('Cosine functions')
legend('2cos(x)','cos(x)','0.5cos(x)')
title('Typical example of multiple plots')
axis([0 2pi -3 3])
Detailed Explanation
You can plot multiple functions on a single graph to compare them directly. By providing multiple (x,y) pairs in the 'plot' function, you can visualize how different functions behave in the same interval. Adding a legend helps differentiate which line belongs to which function, thus making multitasking comparisons clear and efficient.
Examples & Analogies
This is like comparing the prices of three different brands of the same product over a period. If you plot their prices together on the same graph, you can easily observe which brand is cheaper or more expensive at any given time, making decision-making simpler.
Specifying Line Styles and Colors
Chapter 5 of 5
🔒 Unlock Audio Chapter
Sign up and enroll to access the full audio experience
Chapter Content
It is possible to specify line styles, colors, and markers (e.g., circles, plus signs, ...) using the plot command:
plot(x,y,'style_color_marker')
where style_color_marker is a triplet of values from Table 2.3.
Detailed Explanation
Customizing your plot with different line styles, colors, and markers enhances clarity and presentation. The format for customizing is 'style_color_marker', where you can choose options from a predefined set of styles listed in a table, allowing for varied visual differentiation among data sets.
Examples & Analogies
Just like an artist chooses different colors and brush strokes to bring a painting to life, you can choose various styles and colors in MATLAB to improve the visual appeal and convey different messages with your data plots.
Key Concepts
-
2D Plots: Used for visualizing mathematical functions using x and y coordinates.
-
Axis Labels: Important for graph clarity; use xlabel and ylabel to add them.
-
Multiple Datasets: You can plot several datasets in one graph to facilitate comparison.
Examples & Applications
Example of plotting a sine function using plot(x,sin(x)).
Using multiple datasets like y1, y2 in one plot: plot(x,y1,'--', x,y2,'-').
Memory Aids
Interactive tools to help you remember key concepts
Rhymes
Plot a point, connect the line, make your graph look just divine!
Stories
Imagine you are a painter. Each brushstroke on your canvas connects data points in beautiful harmony.
Memory Tools
P.L.E. - Plot, Label, Enhance. This reminds you of the steps: plot your data, label axes, and enhance with titles.
Acronyms
G.P.L.- Graphing, Plotting, Legends - Remember these core concepts for effective plotting!
Flash Cards
Glossary
- Plot
A graphical representation of data points on a coordinate system.
- Vector
An array of numbers typically representing data points in a specific format.
- Axis Labels
Text used to describe the x-axis and y-axis of a graph, improving clarity.
- Title
Text describing the overall content of a graph.
- Legend
A key on a graph that explains what different colors or patterns represent.
Reference links
Supplementary resources to enhance your learning experience.