Undocumented Matlab
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT
  • SERVICES
    • Consulting
    • Development
    • Training
    • Gallery
    • Testimonials
  • PRODUCTS
    • IQML: IQFeed-Matlab connector
    • IB-Matlab: InteractiveBrokers-Matlab connector
    • EODML: EODHistoricalData-Matlab connector
    • Webinars
  • BOOKS
    • Secrets of MATLAB-Java Programming
    • Accelerating MATLAB Performance
    • MATLAB Succinctly
  • ARTICLES
  • ABOUT
    • Policies
  • CONTACT

Customizing axes tick labels

January 24, 2018 5 Comments

In last week’s post, 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. 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 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

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, 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 and here), 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, 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.

Related posts:

  1. Setting axes tick labels format – Matlab plot axes ticks can be customized in a way that will automatically update whenever the tick values change. ...
  2. Customizing axes part 5 – origin crossover and labels – The axes rulers (axles) can be made to cross-over at any x,y location within the chart. ...
  3. Customizing axes rulers – HG2 axes can be customized in numerous useful ways. This article explains how to customize the rulers. ...
  4. Customizing axes part 2 – Matlab HG2 axes can be customized in many different ways. This article explains some of the undocumented aspects. ...
  5. Customizing axes part 3 – Backdrop – Matlab HG2 axes can be customized in many different ways. This article explains some of the undocumented aspects. ...
  6. Customizing axes part 4 – additional properties – Matlab HG2 axes can be customized in many different ways. This article explains some of the undocumented aspects. ...
Handle graphics HG2 Pure Matlab
Print Print
« Previous
Next »
5 Responses
  1. Evgenii January 30, 2018 at 12:23 Reply

    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$'	...
    				  ,'$-5pi/6$'	...
    				  ,'$-2pi/3$'	...
    				  ,'$-pi/2$'	...
    				  ,'$-pi/3$'	...
    				  ,'$pi/6$'	...
    				  ,'$0$'	...
    				  ,'$pi/6$'	...
    				  ,'$pi/$3'	...
    				  ,'$pi/2$'	...
    				  ,'$2pi/3$'	...
    				  ,'$5pi/6$'	...
    				  ,'$pi$'	...
    				 };

    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$' ... ,'$-5pi/6$' ... ,'$-2pi/3$' ... ,'$-pi/2$' ... ,'$-pi/3$' ... ,'$pi/6$' ... ,'$0$' ... ,'$pi/6$' ... ,'$pi/$3' ... ,'$pi/2$' ... ,'$2pi/3$' ... ,'$5pi/6$' ... ,'$pi$' ... };

    • Yair Altman January 30, 2018 at 20:10 Reply

      @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 on the Matlab File Exchange is relatively old (2012) compared to the latest one on Github (2015).

      While this version of plot2svg was last updated in 2015, a more recent version (fork) can be found in https://github.com/kupiqu/plot2svg, and unlike the original version this one seems to be actively maintained.

      • Evgenii February 1, 2018 at 12:46

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

  2. Evgenii March 25, 2018 at 12:09 Reply

    The problem was gone in r2018a.

    • Stefan June 12, 2018 at 15:49 Reply

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

Leave a Reply
HTML tags such as <b> or <i> are accepted.
Wrap code fragments inside <pre lang="matlab"> tags, like this:
<pre lang="matlab">
a = magic(3);
disp(sum(a))
</pre>
I reserve the right to edit/delete comments (read the site policies).
Not all comments will be answered. You can always email me (altmany at gmail) for private consulting.

Click here to cancel reply.

Useful links
  •  Email Yair Altman
  •  Subscribe to new posts (feed)
  •  Subscribe to new posts (reader)
  •  Subscribe to comments (feed)
 
Accelerating MATLAB Performance book
Recent Posts

Speeding-up builtin Matlab functions – part 3

Improving graphics interactivity

Interesting Matlab puzzle – analysis

Interesting Matlab puzzle

Undocumented plot marker types

Matlab toolstrip – part 9 (popup figures)

Matlab toolstrip – part 8 (galleries)

Matlab toolstrip – part 7 (selection controls)

Matlab toolstrip – part 6 (complex controls)

Matlab toolstrip – part 5 (icons)

Matlab toolstrip – part 4 (control customization)

Reverting axes controls in figure toolbar

Matlab toolstrip – part 3 (basic customization)

Matlab toolstrip – part 2 (ToolGroup App)

Matlab toolstrip – part 1

Categories
  • Desktop (45)
  • Figure window (59)
  • Guest bloggers (65)
  • GUI (165)
  • Handle graphics (84)
  • Hidden property (42)
  • Icons (15)
  • Java (174)
  • Listeners (22)
  • Memory (16)
  • Mex (13)
  • Presumed future risk (394)
    • High risk of breaking in future versions (100)
    • Low risk of breaking in future versions (160)
    • Medium risk of breaking in future versions (136)
  • Public presentation (6)
  • Semi-documented feature (10)
  • Semi-documented function (35)
  • Stock Matlab function (140)
  • Toolbox (10)
  • UI controls (52)
  • Uncategorized (13)
  • Undocumented feature (217)
  • Undocumented function (37)
Tags
AppDesigner (9) Callbacks (31) Compiler (10) Desktop (38) Donn Shull (10) Editor (8) Figure (19) FindJObj (27) GUI (141) GUIDE (8) Handle graphics (78) HG2 (34) Hidden property (51) HTML (26) Icons (9) Internal component (39) Java (178) JavaFrame (20) JIDE (19) JMI (8) Listener (17) Malcolm Lidierth (8) MCOS (11) Memory (13) Menubar (9) Mex (14) Optical illusion (11) Performance (78) Profiler (9) Pure Matlab (187) schema (7) schema.class (8) schema.prop (18) Semi-documented feature (6) Semi-documented function (33) Toolbar (14) Toolstrip (13) uicontrol (37) uifigure (8) UIInspect (12) uitable (6) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
Contact us
Captcha image for Custom Contact Forms plugin. You must type the numbers shown in the image
Undocumented Matlab © 2009 - Yair Altman
This website and Octahedron Ltd. are not affiliated with The MathWorks Inc.; MATLAB® is a registered trademark of The MathWorks Inc.
Scroll to top