% >> help plot % plot Linear plot. % plot(X,Y) plots vector Y versus vector X. If X or Y is a matrix, % then the vector is plotted versus the rows or columns of the matrix, % whichever line up. If X is a scalar and Y is a vector, disconnected % line objects are created and plotted as discrete points vertically at % X. % ....... %% %% Axis limits % % To determine axis limits, Matlab uses the minimum and maximum of your % x and y values. In our % current example, our x values were in the range $x \in [-2,2]$ and our y were in the range $x \in % [-1,1]$. We can change this viewing "window", or axes limits using % the axis command. %
This command takes an array argument defined using the square brackets
% [].
% To adjust the limits on our current figure window, to region $[-1, 1]\times [-2, 2]$. we can use
%
axis([-1 1 -2 2])
%%
%
% You can set the axis limits for each axis separately using the commands
% xlim and ylim.
% For example,
%
xlim([-2 2])
%%%
ylim([-1 1])
%%
%
% restores the axis to their original settings.
%
% To retrieve these values from the current figure window, we just use the
% xlim and ylim without
% arguments.
%
xlim
%%
ylim
%%
%
% You may also want to preserve the aspect ratio of the plot, so that
% visually, 1 unit of distance on the x-axis is the same as 1-unit on the
% y-axis. The command
%
daspect([1 1 1])
%%
%
% is one way to do this. The first two arguments indicate the relative
% ratio of the x and y axis. The third argument is for the z-axis, and
% can be always set to 1 for 2d plots.
%
% >> help axis % axis Control axis scaling and appearance. % axis([XMIN XMAX YMIN YMAX]) sets scaling for the x- and y-axes % on the current plot. % .................... %%
% >> help xlim % xlim X limits. % XL = xlim gets the x limits of the current axes. % xlim([XMIN XMAX]) sets the x limits. % .................... %%
% >> help ylim % ylim Y limits. % YL = ylim gets the y limits of the current axes. % ylim([YMIN YMAX]) sets the y limits. % .................... %% %% Adding additional plots to an existing window % % Very often, we wish to add additional curves to existing plots. This % be easily done with the hold command. % First, we will clear the current figure window, re-draw our previous % plot, "hold" the state of the first plot, and then add a second plot. % clf %%% plot(x,y) %%% hold on %%% plot(2*x,y/2) %% % % To plot a curve in red instead of the default blue, add a color % attribute to the plot command : % plot(2*x,y/2,'r') %% % % Also available are different line types, e.g. dashed lines, dotted % lines, and so on. To use these, you can augment the color command % with a line style. For example, to get a dashed line, use % the '--' line attribute. Using an % additional argument in this string, we can specify both the color and % the line type : % plot(4*x,y/4,'k--') %% Adding symbols to the plot % % We can add symbols to the plot as well. Suppose we want to put a % symbols at each maximum value and minimum of the function % $$ % g(x) = \cos(4 \pi x)/4 \\ % g'(x) = -\pi\sin(4\pi x) % $$. % This function has a minimum or maximum whenever % $g'(x) = 0$, i.e. at % $$x_{minmax} = [-1.5, -1, -0.5, 0, 0.5, 1, 1.5]$$ % so we will create a simple array to store these values: % xminmax = [-1.5, -1, -0.5, 0, 0.5, 1, 1.5]; %% % % We can now plot a symbol at each $(x,y)$ % plot(4*xminmax, cos(2*pi*xminmax)/4,'k.','markersize',30) %% % % Note that particulary for the '.' symbol, it is useful (if not critical) % to set the marker size. Otherwise, the '.' is too small to see on the % plot. %
% >> help plot % ........................ % Various line types, plot symbols and colors may be obtained with % plot(X,Y,S) where S is a character string made from one element % from any or all the following 3 columns: % % b blue . point - solid % g green o circle : dotted % r red x x-mark -. dashdot % c cyan + plus -- dashed % m magenta * star (none) no line % y yellow s square % k black d diamond % w white v triangle (down) % ^ triangle (up) % < triangle (left) % > triangle (right) % p pentagram % h hexagram % ...................... %% %% Adding a title and axis labels % % A plot is not complete without a title, and axes labels. Use the % following commands to add these items to your plot. % xlabel('x') %%% ylabel('y') %%% title('A simple plot') %% % % You can change the font-size (among other things) by passing additional % arguments to the xlabel, % ylabel and % title commands: % xlabel('x','fontsize',18) %%% ylabel('f(x)','fontsize',18) %%% set(gca,'fontsize',18) %%% title('A simple function','fontsize',18,'fontweight','bold') %% Printing the figure window % % Eventually, you will want to print you plot for use in other documents, % such as Word, Latex, or a webpage. You can produce an image file in any % number of formats. A format that works well for most purposes is the % % PNG (Portable Graphics Format). To print out your figure using this % format, use the print command: % print -dpng simple_function.png %% % % A list of available formats can be found by looking at help on % print command. %
% >> help print % print Print figure or model. Save to disk as image or MATLAB file. % ...................... % print -device -options filename % If you specify a filename, MATLAB directs output to a file instead of % a printer. print adds the appropriate file extension if you do not % specify one. % ..................... % Built-in MATLAB Drivers: % ..................... % -depsc2 % Encapsulated Level 2 Color PostScript % ..................... % -djpeg%% JPEG image, quality level of nn (figures only) % E.g., -djpeg90 gives a quality level of 90. % Quality level defaults to 75 if nn is omitted. % ..................... % -dtiff % TIFF with packbits (lossless run-length encoding) % compression (figures only) % ..................... % -dpng % Portable Network Graphic 24-bit truecolor image % (figures only) %
Many of the commands discussed above for adding titles and so on to % your plots can be done from menu items in the figure % window. These are handy if you plan to make a plot only % once. But often, you will run a simulation several times, % and would like all of your plot attributes to be added automatically. % For this reason, we have discussed mainly the command line % methods for modifying plots. % %% Clearing and closing graphics windows % % To clear the graphics window you can use the clf command, which stands for "clear figure". % This only removes any plotting elements from the current figure window % but does not close the window itself. %
% >> clf %%
To close out a figure window % can use the close command. %
% >> close all %%
You can selectively close figure windows by supplying an argument to % the close command: %
% >> close(1) %% %% The EZ way to plot % % The easiest way to plot a function using Matlab is to use the ezplot command. At its simplest, this command % requires a single argument, the function handle. % close all; %% f = @(x) exp(cos(x)).*sin(x); %% ezplot(f); %% % % By default, ezplots plots over the range $[-2\pi, 2\pi]$. To specify a custom range over % which to plot the function, pass in two additional arguments, the left % and right endpoints of the range in an two element array. % a = -pi/2; %% b = 3*pi/2; %% ezplot(f,[a b]); %% % % Using ezplot, you can still add titles and axes % labels to your plots as before. In fact, it is possible to change most % aspects of the plot, such as the line type and color, using what is known % as "Handle Graphics". %
Compare your answers with the solutions.
%