- Undocumented Matlab - https://undocumentedmatlab.com -

Customizing axes tick labels

Posted By Yair Altman On January 24, 2018 | 5 Comments

In last week’s post [3], I discussed various ways to customize bar/histogram plots, including customization of the tick labels. While some of the customizations that I discussed indeed rely on undocumented properties/features, many Matlab users are not aware that tick labels can be individually customized, and that this is a fully documented/supported functionality [4]. This relies on the fact that the default axes TickLabelInterpreter property value is 'tex', which supports a wide range of font customizations, individually for each label. This includes any combination of symbols, superscript, subscript, bold, italic, slanted, face-name, font-size and color – even intermixed within a single label. Since tex is the default interpreter, we don’t need any special preparation – simply set the relevant X/Y/ZTickLabel string to include the relevant tex markup.

To illustrate this, have a look at the following excellent answer [5] by user Ubi on Stack Overflow:

Axes with Tex-customized tick labels

Axes with Tex-customized tick labels

plot(1:10, rand(1,10))
ax = gca;
 
% Simply color an XTickLabel
ax.XTickLabel{3} = ['\color{red}' ax.XTickLabel{3}];
 
% Use TeX symbols
ax.XTickLabel{4} = '\color{blue} \uparrow';
 
% Use multiple colors in one XTickLabel
ax.XTickLabel{5} = '\color[rgb]{0,1,0}green\color{orange}?';
 
% Color YTickLabels with colormap
nColors = numel(ax.YTickLabel);
cm = jet(nColors);
for i = 1:nColors
    ax.YTickLabel{i} = sprintf('\\color[rgb]{%f,%f,%f}%s', cm(i,:), ax.YTickLabel{i});
end

In addition to 'tex', we can also set the axes object’s TickLabelInterpreter to 'latex' for a Latex interpreter, or 'none' if we want to use no string interpretation at all.

As I showed in last week’s post [3], we can control the gap between the tick labels and the axle line, using the Ruler object’s undocumented TickLabelGapOffset, TickLabelGapMultiplier properties.

Also, as I explained in other posts (here [6] and here [7]), we can also control the display of the secondary axle label (typically exponent or units) using the Ruler’s similarly-undocumented SecondaryLabel property. Note that the related Ruler’s Exponent property is documented/supported [8], but simply sets a basic exponent label (e.g., '\times10^{6}' when Exponent==6) – to set a custom label string (e.g., '\it\color{gray}Millions'), or to modify its other properties (position, alignment etc.), we should use SecondaryLabel.

Categories: Handle graphics, Low risk of breaking in future versions, Stock Matlab function


5 Comments (Open | Close)

5 Comments To "Customizing axes tick labels"

#1 Comment By Evgenii On January 30, 2018 @ 12:23 pm

I try to save my graphics like scalable vector graphics but the Tex or LaTex string doesn’t look right. Can you help me?

x				= 0 : pi/360 : 2*pi;
y				= sin(x+pi);
hFigure				= figure;
hAxes				= subplot(1,1,1);
hLine				= plot(x,y);
hAxes.XGrid			= 'on';
hAxes.YGrid			= 'on';
hAxes.TickLabelInterpreter	= 'Latex';
hAxes.XLim			= [0 2*pi];
hAxes.XTick			= 0 : pi/6 : 2*pi;
hAxes.XTickLabel		= {		...
				   '$-\pi$'	...
				  ,'$-5\pi/6$'	...
				  ,'$-2\pi/3$'	...
				  ,'$-\pi/2$'	...
				  ,'$-\pi/3$'	...
				  ,'$\pi/6$'	...
				  ,'$0$'	...
				  ,'$\pi/6$'	...
				  ,'$\pi/$3'	...
				  ,'$\pi/2$'	...
				  ,'$2\pi/3$'	...
				  ,'$5\pi/6$'	...
				  ,'$\pi$'	...
				 };

#2 Comment By Yair Altman On January 30, 2018 @ 8:10 pm

@Evgenii – this is a bug in Matlab. Please report it to MathWorks. Until the bug is fixed, you can use the plot2svg utility, which generates nice-looking results without the bug.

Note: the version of plot2svg [14] is relatively old (2012) compared to the [15] (2015).

While this version of plot2svg was last updated in 2015, a more recent version (fork) can be found in [16], and unlike the original version this one seems to be actively maintained.

#3 Comment By Evgenii On February 1, 2018 @ 12:46 pm

plot2svg show the symbol pi but it’s unpleasant.
I reported about the bug to MathWorks but the response wouldn’t be fast.

#4 Comment By Evgenii On March 25, 2018 @ 12:09 pm

The problem was gone in r2018a.

#5 Comment By Stefan On June 12, 2018 @ 3:49 pm

No, saveas(gcf,'filename.svg') still yields axis ticks not formatted with latex. Or what are you referring to?


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/blog/customizing-axes-tick-labels

URLs in this post:

[1] Image: http://undocumentedmatlab.com/feed/

[2] email feed: http://undocumentedmatlab.com/subscribe_email.html

[3] last week’s post: https://undocumentedmatlab.com/blog/customizing-histogram-plots

[4] fully documented/supported functionality: https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.axes-properties.html#property_d119e51283

[5] excellent answer: https://stackoverflow.com/a/46181211/233829

[6] here: https://undocumentedmatlab.com/blog/customizing-axes-part-5-origin-crossover-and-labels

[7] here: https://undocumentedmatlab.com/blog/customizing-axes-rulers#Exponent

[8] documented/supported: https://www.mathworks.com/help/matlab/ref/matlab.graphics.axis.decorator.numericruler-properties.html

[9] HG2 update : https://undocumentedmatlab.com/blog/hg2-update

[10] Performance: accessing handle properties : https://undocumentedmatlab.com/blog/performance-accessing-handle-properties

[11] Customizing axes rulers : https://undocumentedmatlab.com/blog/customizing-axes-rulers

[12] Plot legend title : https://undocumentedmatlab.com/blog/plot-legend-title

[13] copyobj behavior change in HG2 : https://undocumentedmatlab.com/blog/copyobj-behavior-change-in-hg2

[14] : https://www.mathworks.com/matlabcentral/fileexchange/7401-scalable-vector-graphics--svg--export-of-figures

[15] : https://github.com/jschwizer99/plot2svg

[16] : https://github.com/kupiqu/plot2svg

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.