www.gusucode.com > graphics 案例源码程序 matlab代码 > graphics/SpecifyAxisTickValuesAndLabelsExample.m
%% Specify Axis Tick Values and Labels % Customizing the tick labels along the _x_-axis and _y_-axis can help % highlight particular aspects of your data. These examples show some % common customizations, such as modifying the tick values and labels along % each axis, rotating the tick labels, and controlling the tick label % formatting. %% Change Tick Value Locations and Labels % Create a line plot and change the tick value locations and associated % labels. % % Create x as 200 linearly spaced values between -10 and 10. Create y as % the cosine of x. Plot the data. x = linspace(-10,10,200); y = cos(x); plot(x,y) %% % Change the tick value locations along the _x_-axis and _y_-axis. Also, % change the labels associated with each tick value along the _x_-axis. % Specify the tick values as a vector of increating values. Specify the % labels using a cell array of character vectors. To include special % characters or Greek letters in the labels, use TeX markup, such as |\pi| % for the $\pi$ symbol. xticks([-3*pi -2*pi -pi 0 pi 2*pi 3*pi]) xticklabels({'-3\pi','-2\pi','-\pi','0','\pi','2\pi','3\pi'}) yticks([-1 -0.5 0 0.5 1]) %% % For releases prior to R2016b, set the tick values and labels using the % |XTick|, |XTickLabel|, |YTick|, and |YTickLabel| properties of the axes % object. For example, assign the axes object to a variable, such as |ax = % gca|. Then set the |XTick| property using dot notation, such as |ax.XTick % = [-3*pi -2*pi -pi 0 pi 2*pi 3*pi]|. For releases prior to R2014b, % use the |set| function to set the property instead. %% Rotate Tick Labels % Create a scatter plot and rotate the tick values along each axis. Specify % the rotation as a scalar value. Positive values indicate counterclockwise % rotation. x = 1000*rand(40,1); y = rand(40,1); scatter(x,y) xtickangle(45) ytickangle(90) %% % For releases prior to R2016b, specify the rotation using the % |XTickLabelRotation| and |YTickLabelRotation| properties of the axes % object. For example, assign the axes object to a variable, such as |ax = % gca|. Then set the |XTickLabelRotation| property using dot notation, such % as |ax.XTickLabelRotation = 45|. For releases prior to R2014b, use % the |set| function to set the property instead. % %% Change Tick Label Formatting % Create a stem chart and display the tick label values along the _y_-axis % as U.S. dollar values. profit = [20 40 50 40 50 60 70 60 70 60 60 70 80 90]; stem(profit) xlim([0 15]) ytickformat('usd') %% % For more control over the formatting, specify a custom format. For % example, show one decimal value in the _x_-axis tick labels. Display the % _y_-axis tick labels as British Pounds using |'\xA3%.2f'|. The option % |\xA3| indicates the Unicode character for the Pound symbol. xtickformat('%.1f') ytickformat('\xA3%.2f') %% % For more information on specifying a custom format, see the % <docid:matlab_ref.bu6ia4o> function. %% Control Value in Exponent Label % Plot data with _y_ values that range between -15,000 and 15,000. By % default, the _y_-axis tick labels use exponential notation with an % exponent value of 4 and a base of 10. x = linspace(0,5,1000); y = 100*exp(x).*sin(20*x); plot(x,y) %% % Change the exponent value to 2. Set the |Exponent| property of the % numeric ruler associated with the _y_-axis. Access the numeric ruler % through the |YAxis| property of the axes object. The exponent label and % the tick labels change accordingly. ax = gca; ax.YAxis.Exponent = 2; %% % Change the exponent value to 0 so that the tick labels do not use % exponential notation. ax.YAxis.Exponent = 0;