We always need figure to demonstrate the data we have.
Here gives an example of the basic way to use figure and plot:
% Start
x = -100:0.1:100;
y1 = x + 1;
y2 = x .* x;
figure(1); % create a new figure 1
plot(x, y1); % plot two curves in one figure
hold on; % To keep current curve appear
plot(x, y2);
% you can actually use the following one, it works the same
% plot(x,y1, x,y2);
legend('y1', 'y2'); % add lengeds
title('Plot Demonstration'); % title of the plot
xlabel('x'); ylabel('y') % label of axis
axis([-5 5 -10 10]); % scale of axes [xmin xmax ymin ymax]
set(gca, 'XTick', (-5: 1: 5)); % set the step of axes [min: step: max]
set(gca, 'YTick', (-10: 2: 10));
grid on; % plot with grid on
% Other constructing method
x = 1:5:100;
Two = 3 * x - 2;
figure('Name', 'Two', 'NumberTitle', 'off'); % create a new figure named 'Two' without number title
plot(x, Two, ... % some parameters to change a curve's properties
':', ...
'Color', 'b', ...
'LineWidth', 1, ...
'Marker', '*', ...
'MarkerFaceColor', [1, 0.1, 0.1], ...
'MarkerEdgeColor', [1, 0.1, 0.1], ...
'MarkerSize', 3 ...
);
% To use subplot to have multiple plots in one figure
k = linspace(0, 9.42, 100); % k is a vector including 100 numbers from 0 to 3pi
z0 = sin(k);
z1 = cos(k);
figure('Name', 'Subplot');
subplot(2,2, 1); % a subplot constructed as 2 by 2 matrix and we use the first one
plot(k, z0);
subplot(2,2, 2); % same as before but using the second one this time
plot(k, z1);
subplot(2,2, [3,4]); % using 3 and 4 together to plot the last one
plot(k, z0 + z1);
Appendix may be needed:
Marker | Description |
---|---|
o | Circle |
+ | Plus sign |
* | Asterisk |
. | Point |
x | Cross |
s | Square |
d | Diamond |
^ | Upward-pointing triangle |
v | Downward-pointing triangle |
> | Right-pointing triangle |
< | Left-pointing triangle |
p | Pentagram |
h | Hexagram |
Color | Description |
---|---|
| yellow |
| magenta |
| cyan |
| red |
| green |
| blue |
| white |
| black |
Line Style | Description |
---|---|
- | Solid line (default) |
-- | Dashed line |
: | Dotted line |
-. | Dash-dot line |