www.gusucode.com > graphics 案例源码程序 matlab代码 > graphics/AddTitleAxisLabelsAndLegendExample.m
%% Add Title, Axis Labels, and Legend to Graph % This example shows how to add a title, axis labels and a legend to a % graph using the |title|, |xlabel|, |ylabel| and |legend| functions. By % default, these functions add the text to the current axes. The current % axes is typically the last axes created or the last axes clicked with the % mouse. %% Create Simple Line Plot % Define |x| as 100 linearly spaced values between $-2\pi$ and $2\pi$. % Define |y1| and |y2| as sine and cosine values of |x|. Create a line plot % of both sets of data. x = linspace(-2*pi,2*pi,100); y1 = sin(x); y2 = cos(x); figure plot(x,y1,x,y2) %% Add Title % Add a title to the graph using the |title| function. To display Greek % symbols in a title, use TeX markup. Use the TeX markup, |\pi|, to % display the Greek symbol $\pi$. title('Graph of Sine and Cosine Between -2\pi and 2\pi') %% Add Axis Labels % Add axis labels to the graph using the |xlabel| and |ylabel| functions. xlabel('-2\pi < x < 2\pi') % x-axis label ylabel('sine and cosine values') % y-axis label %% Add Legend % Add a legend to the graph identifying each data set using the |legend| % function. Specify legend descriptions in the order that you plot the % lines. legend('y = sin(x)','y = cos(x)') %% Specify Legend Location % Specify the location of the legend on the graph by setting its location % using one of the eight cardinal or intercardinal directions. Display the % legend at the bottom left corner of the axes by specifying the location % as |'southwest'|. legend('y = sin(x)','y = cos(x)','Location','southwest') %% % To display the legend outside the axes, append |outside| to any of the % directions, for example, |'southwestoutside'|.