Introduction
In this tutorial we will look at some basic functionalities of matlab figures. We will see some examples and understand how to use functions such as plot and stem.
- figure creates a new figure window using default property values. The resulting figure is the current figure.
Example:
- figure(Name,Value) modifies properties of the figure using one or more name-value pair arguments. For example, figure('Color','Red') sets the background color to red (same as figure('Color','r')
Creating one discrete and one 2D line plot in the same figure
figure("Color", "c"); %adds cyan color at the background of matlab % figure
%Creates a vector of 100 evenly spaced points in the interval
%[-5,5].
Y = linspace(-5,5,50);
plot(Y, "r:") % plots Y against an implicit set of x-coordinates.
% adds red color to line (from r) and makes it dotted (from :)
hold on % use hold on to add a second plot
stem(Y, "c") % plott discrete sequence data
% c makes line have cyan color
Adding grid to your figures:
- grid= Display or hide axes grid lines
1) grid on = displays the major grid lines for the current axes
2) grid off = removes all grid lines from the current axes or chart.
3) grid minor = Display the minor grid lines.
scatter = Scatter plot
tiledlayout = Create tiled chart layout
Example:
tiledlayout(m,n) = creates a tiled chart layout for displaying multiple plots in the current figure. The layout has a fixed m-by-n tile arrangement that can display up to m*n plots.
Example no 2:
clc;
clear;
figure("Color", "c");
x = linspace(0,3*pi,200); %Create a vector of 100 points in the interval [0,3π,200].
y = cos(x) + rand(1,200);
t = tiledlayout(2,2);
title(t,'Title of our figure') % add title
xlabel(t,'X label')% add x label
ylabel(t,'Y label')% add y label
t.TileSpacing = 'compact'; %Decrease the amount of space between the tiles by setting the TileSpacing property to 'compact'
t.Padding = 'compact'; %decrease the space between the edges of the layout and the edges of the figure by setting the Padding property to 'compact'.
% add your plots in the same figure:
ax1 = nexttile;
ax1.XColor = [1 1 1];
ax1.YColor = [1 0 0];
scatter(ax1,x,y);
grid on
title('Sample 1');
ax2 = nexttile;
scatter(ax2,x,y,'filled','d');
title('Sample 2');
nexttile(t);
bar([10 22 31 43 52])
title('Sample 3');
nexttile(t);
x = linspace(-10,10,200); %Create x as 200 linearly spaced values between -10 and 10.
y = cos(x); % Create y as the cosine of x.
plot(x,y)
%Change the tick value locations along the x-axis and y-axis.
%Specify the locations as a vector of increasing values. The values do not need to be evenly spaced.
xticks([-3*pi -2*pi -pi 0 pi 2*pi 3*pi])
xticklabels({'-3\pi','-2\pi','-\pi','0','\pi','2\pi','3\pi'}) %Also, change the labels associated with each tick value along the x-axis. To include special characters or Greek letters in the labels, use TeX markup, such as \pi for the π symbol
yticks([-1 -0.8 -0.2 0 0.2 0.8 1])
Top comments (0)