MATLAB Answers

3

How do I control axis tick labels, limits, and axes tick locations?

Asked by MathWorks Support Team on 7 Feb 2011
Latest activity Answered by morgan alice on 18 Nov 2019 at 10:55
I would like to know if I have to set the XTickLabel, YTickLabel, ZTickLabel, XTick, YTick, and ZTick properties.

3 Answers

Answer by MathWorks Support Team on 3 Jan 2019
Edited by MathWorks Support Team on 3 Jan 2019
 Accepted Answer

You can control the axis limits using the "xlim", "ylim", and "zlim" functions. Pass the functions a two- element vector of the form [min max]. For example:
x = linspace(0,2*pi);
y = sin(x);
plot(x,y);
xlim([0 2*pi])
ylim([-1.5 1.5])
You can control the placement of the tick marks along an axis using the "xticks", "yticks", and "zticks" functions. Specify the tick mark locations as a vector of increasing values. The values do not need to be evenly spaced. For example:
xticks([0 pi 2*pi])
yticks([-1 0 1])
To control the labels associated with each tick mark, use the "xticklabels", "yticklabels", and "zticklabels" functions. Specify the labels using a cell array of character vectors. If you do not want tick labels to show, then specify an empty cell array {}.To include special characters or Greek letters in the labels, use TeX markup, such as \pi. For example:
xticklabels({'0','\pi','2\pi'})
yticklabels({'min','y = 0','max'})
You also can rotate the tick labels and change the format using functions such as "xtickangle" and "xtickformat". For more information about these functions, see:
If you are using R2016a or earlier, you can specify the limits, tick values, and tick labels by setting properties of the Axes object. For example, to modify the values in the x direction, use the XLim, XLimMode, XTick, XTickMode, XTickLabel, and XTickLabelMode properties, such as:
ax = gca;
ax.XLim = [0 2*pi];
ax.XTick = [0 pi 2*pi];
For more information about these properties, see:

  1 Comment

Even when I copy the same code (as provided by mathworks team) and try to run it , I get following error message:
Values plotted against x-axis must be datetime values. To create datetime values, use the DATETIME function.
Am i doing anything wrong?

Sign in to comment.


Answer by Bradley Thomson on 25 Nov 2015

set(gca,'XTickLabel',['0';' ';'1';' ';'2';' ';'3';' ';'4'])
Note an issue with this formulation is that floating-point tick labels yield an error: Error using vertcat Dimensions of matrices being concatenated are not consistent.

  1 Comment

I think that what you're referring to is what this part ...
set(gca,'XTickLabel',['1 ';'100'])
% Alternatively, use a cell array of strings:
set(gca,'XTickLabel',{'1','100'})
... is trying to explain.
If you do this with a 2D array of characters (the first version), then you need to add spaces so that all of the rows have the same number of columns.
If you do this with a cell array (the second version), then you don't need to add spaces.
Either way works. Choose the one you prefer.

Sign in to comment.


Answer by morgan alice 4 minutes ago

hey you can use customized tickes. the Matplotlib is a default tick locator and formatters that is designed to generate. the representation is be like; to recall the situation and each figure represent everything that appear on a plot and contain the one or more axes. ones i received an assignment of cipd writers Saudia Arabia student, as cipd also about something like this so i give them this solution.
the tick marks have no exception. each axes hase attributes of x axis and y axis.
there is also a concept of major and minor ticks;
import matplotlib.pyplot as plt
plt.style.use('classic')
%matplotlib inline
import numpy as np
ax = plt.axes(xscale='log', yscale='log')
ax.grid();
print(ax.xaxis.get_major_locator())
print(ax.xaxis.get_minor_locator())
print(ax.xaxis.get_major_formatter())
print(ax.xaxis.get_minor_formatter())

  0 Comments

Sign in to comment.