Matlab adjust axis tick labels, limits, and tick locations

来源:互联网 发布:python编程入门下载 编辑:程序博客网 时间:2024/06/01 15:55

From: https://cn.mathworks.com/matlabcentral/answers/92565-how-do-i-control-axis-tick-labels-limits-and-axes-tick-locations

The following example can be used as a resource and guide for adjusting axis tick labels, limits, and tick locations.

If you are plotting earnings versus time, consider labeling your X-tick marks with weeks, months, or even years. Below is a list of properties that control the X-tick locations and labels. These are properties of axes objects.

 

 XLim XLimMode                       [{auto}|manual] XTick XTickMode                      [{auto}|manual] XTickLabel 

NOTE: The Y and Z axes have similar properties that start with Y or Z respectively.

The 'XLim' property is used to store the limits of the X-axis. By default, MATLAB chooses the limits; however, you can specify the limits using "axis([xmin xmax ymin ymax zmin zmax])" or "set(gca,XLim,[xmin xmax])". Setting this property will automatically change the 'XLimMode' to 'manual'.

'XLimMode' is used to determine if MATLAB is controlling the X-limits or if you are controlling them. By default, it is set to 'auto', which implies that MATLAB chooses the limits. To prevent MATLAB from changing the limits when the figure is resized or printed, set this property to 'manual'. If you set the 'XLim' property, or use the AXIS command, 'XLimMode' is automatically set to 'manual'.

'XTick' is the property in which MATLAB stores the location of the X-tick marks. Generally, this property is used by MATLAB; however, you can set this property so that only the desired tick marks are drawn. Setting this property automatically changes the 'XTickMode' property to manual.

'XTickMode' is used to determine whether MATLAB or you control the tick locations. By default, it is set to 'auto', which implies that MATLAB controls the locations of the tick marks. To prevent MATLAB from changing the tick locations or number of ticks when the figure is resized or printed, change this property to 'manual'. If 'XTick' is set by you, this property is automatically set to 'manual'.

'XTickLabel' is the property in which MATLAB stores the strings used to label the tick marks. Normally, this property contains the string representation of the 'XTick' property. For example, if 'XTick' contains the vector [2 4 6 8], then 'XTickLabel' contains the following string array:

 

 2 4 6 8

Usually, the number of rows in 'XTickLabel' is equal to the number of tick marks. If this is not true, then MATLAB will cycle through the X-tick labels to label each of the tick marks. For example, if the previous string array only contained the first two rows, the ticks along the X-axis would be labelled 2--4--2--4.

'XTickLabel' can be a cell array of strings, or a string array. If you use a string array, you must pay attention to the fact that every row must contain the same number of columns. So, if you change the 'XTickLabel' to contain 1 and 100, you will have to pad the 1 with spaces. For example:

 

set(gca,'XTickLabel',['1  ';'100'])
% Alternatively, use a cell array of strings:set(gca,'XTickLabel',{'1','100'})

Now that we have established the properties required to change the tick location and labels, let's work through an example:

 

% Generate a random vector of earning that can range from% $0 - $2000.  Each element of earnings correspond to the% earnings for a given month.earnings = 1000 + 1000*rand(1,12) - 1000*rand(1,12);
% Generate a bar plot of the earnings with 12 bins % (1 bin per month)bar(earnings);
% Set the XLim so that it ranges from .5 - 12.5.  % The reason that it starts at .5 and ends at 12.5 is% because each bin is .5 data units wide.set(gca,'XLim',[.5 12.5]);% This automatically sets the                          % XLimMode to manual.
% Set XTick so that only the integer values that % range from 0.5 - 12.5 are used.set(gca,'XTick',[1:12])  % This automatically sets                          % the XTickMode to manual.
% Set the XTickLabel so that abbreviations for the% months are used.months = ['Jan';          'Feb';          'Mar';          'Apr';          'May';          'Jun';          'Jul';          'Aug';          'Sep';          'Oct';          'Nov';          'Dec'];set(gca,'XTickLabel',months)

Remember, the Y- and Z-axes both have properties similar to the ones described in this solution.

Another thing you may want to do is have fewer tick labels than tick marks. The following code demonstrates how to do this:

 

plot(0:4,0:4)set(gca,'XLim',[0 4])set(gca,'XTick',[0:0.5:4])set(gca,'XTickLabel',['0';' ';'1';' ';'2';' ';'3';' ';'4'])

---------------------------------EDIT ------------------------------

New helper functions have been introduced in MATLAB R2016b to control the following properties -

 

1. xlim - Set or query x-axis limits

2. xtickformat - Specify x-axis tick label format

3. xticklabels - Set or query x-axis tick labels

4. xticks - Set or query x-axis tick values

5. xtickangle - Rotate x-axis tick labels

 

The same example as above using the latest helper functions - 

 

% Generate a random vector of earning that can range from% $0 - $2000.  Each element of earnings correspond to the% earnings for a given month.earnings = 1000 + 1000*rand(1,12) - 1000*rand(1,12);
% Generate a bar plot of the earnings with 12 bins% (1 bin per month)bar(earnings);
% Set the XLim so that it ranges from .5 - 12.5. % The reason that it starts at .5 and ends at 12.5 is% because each bin is .5 data units wide.                           xlim([.5 12.5]); % This automatically sets the                            % XLimMode to manual.
% Set XTick so that only the integer values that% range from 0.5 - 12.5 are used.xticks(1:12);  % This automatically sets                           % the XTickMode to manual.
% Set the xticklabels so that abbreviations for the% months are used.months = ['Jan';            'Feb';            'Mar';            'Apr';            'May';            'Jun';            'Jul';            'Aug';            'Sep';            'Oct';            'Nov';            'Dec'];xticklabels(months);
% Set the xtickangle to rotate the x-axis tick lablesxtickangle(45);
The second example to have fewer tick labels than tick marks can be re-written as
​plot(0:4,0:4)xlim([0 4])xticks([0:0.5:4])xticklabels(['0';' ';'1';' ';'2';' ';'3';' ';'4'])

0 0
原创粉丝点击