Borrar filtros
Borrar filtros

How to manually enter the length of axis tick marks?

301 visualizaciones (últimos 30 días)
Emily Smith
Emily Smith el 6 de Oct. de 2016
Editada: sriedl el 9 de Abr. de 2021
I want to make the tick marks on the x and y axis 1 cm apart. Or at least a specific numeric value that will not change or be resized based on the size on the Figure window.
I used set(gca,'TickLength',[0.01,0.01]); to try and at least set a numerical value for the tick lengths but it didn't work. The spacing between the tick marks on the x axis are much farther apart.
Thank You!

Respuestas (2)

Marc Jakobi
Marc Jakobi el 6 de Oct. de 2016
Setting the TickLength changes the length of the tick markers, not the distance between them. Try
set(gca,'TickLength',[0.1, 0.01])
on an empty axes for an exaggerated demonstration of what it does.
to change the distance between ticks, use
set(gca,'XTick', xticks) %xticks is a double vector with the tick positions
set(gca,'YTick', yticks) %ytick is a double vector with the tick positions
To adjust the tick labels, use
set(gca,'XTickLabel',xt) %xt is a cell array with the labels (empty cell for removing label)
You cannot specify the TickLength in centimeters. However, it is normalized to the axes height/width. And you can specify the axes size in centimeters with
set(gca, 'Units', 'centimeters')
So if ax.TickLength = [1, 0.01], the length of the XTicks would be equivalent to ax.Position(4) [the height in cm] and the length of the YTicks would be equivalent to ax.Position(3) [the width in cm].
So, to get the desired TickLength in cm:
set(gca,'Position', 'centimeters')
pos = get(gca,'Position');
height_cm = pos(4);
desired_length = 1; %cm
normalized_length = desired_length./height_cm;
set(gca,'TickLength', [normalized_length, 0.01])
Note that Matlab has problems when exporting figures to other formats. A good function for exporting figures in the correct size is export_fig on FileExchange.
  1 comentario
sriedl
sriedl el 9 de Abr. de 2021
Editada: sriedl el 9 de Abr. de 2021
The 'TickLength' Property takes values in 'units normalized relative to the longest of the visible axis'. If you want to change the tick length but do not know whether the X axis or the Y axis is longer, using ax.Position(4) or ax.Position(3) will not necessarily return the required value. It is better to use max(ax.Position(3:4)) to determine the longest length of the axes:
desired_length = 1; %tick length in cm
ax = gca;
ax.Units = 'centimeters';
ax.TickLength(1) = desired_length / max(ax.Position(3:4)); % set 2D TickLength

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 6 de Oct. de 2016
The tick positions for an axes are always in data units, never in cm or pixels.
If resizing can only happen by changing the size of the figure window, then set the figure SizeChangedFcn callback (or ResizeFcn for older versions) to peek into the axes and determine its new size and set the tick positions by calculating what 1 cm would be for it.
If the axes size is changing because of a figure size change then that implies you have the Units property for the axes set to 'normalized'. To determine the new size of the axes in cm, temporarily set the axes Units to 'cm', retrieve the Position property, and set the axes Units back again. You will also need the axes XLim and YLim properties (which will always be in data units) in order to figure out the data coordinates at which those 1 cm ticks are to go.
If resizing the axes might happen by the user dragging the axes corner, then that will not be noticed by the figure SizeChangedFcn or ResizeFcn, which is only used if the figure changes. Unfortunately there is no axes SizeChangedFcn, so to handle this you might need to use addlistener to listen to the axes Position (or OuterPosition) 'PostSet'.
  1 comentario
Emily Smith
Emily Smith el 6 de Oct. de 2016
thank you for your feedback. what is the syntax for setting the length of the tick marks? And what is the syntax for setting the axes Units?

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Properties en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by