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 (email)
  •  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
ActiveX (6) 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) uitools (20) Undocumented feature (187) Undocumented function (37) Undocumented property (20)
Recent Comments
  • Josh (22 hours 39 minutes ago): Dear Yair, Small typo; you wrote >>Move lines up or down – CTRL + ALT + UP or DOWN allows you to move selected lines up or down but it’s actually ALT+SHIFT then UP/DOWN...
  • Yair Altman (7 days 17 hours ago): You can try to increase the Java heap memory size in the Matlab preferences (General => Java Heap Memory). Any changes to the settings will only take effect after restarting...
  • Thomas (7 days 18 hours ago): Hello, thanks for sharing! I currently receive the following memory error message when using savezip with a big object (Matlab R2020b, Windows 10). Error using...
  • Yair Altman (11 days 2 hours ago): Yerlan – either use menuItem1.setEnabled(0) or set(menuItem1,'Enabled',0)
  • Manzn (11 days 4 hours ago): Thank you man! you saved me, when there was no more light 😀
  • Yerlan (12 days 9 hours ago): Hello Mr. Altman, how can I disable menu items in the context menu? E.g. when I am trying to do this: menuItems = jmenu.getSubElements; menuItem1 = menuItems(1);...
  • Yair Altman (13 days 1 hour ago): Thanks for the note Eric – you forgot the crucial call to jTable.setLabelTable(labelTabl e) – I added it into your code snippet above.
  • Erik (14 days 12 hours ago): Thank you for this — I don’t know if this has been mentioned anywhere else before, but it could be useful to mention how to add custom labels to a jslider. I did the...
  • turhv (27 days 7 hours ago): very nice! work perfectly to me in MATLAB 2019a. thanks!!!
  • Jianfei (60 days 11 hours ago): I have tried the MathWorks CheckBoxList in Matlab 2015b. For whatever the reason, I can’t change the font properties. I can change the font color, but changing font properties...
  • Donato Coppola (66 days 6 hours ago): Hi Yair, I have a problem with the double format. When I run the treeTable function, the numbers in double format cells are displayed with comma as decimal separator. How can...
  • Kim (71 days 20 hours ago): Yair, the following didn’t work for me either: jh.setBorderPainted(false); Regards, Kim
  • Adil (73 days 22 hours ago): Thank you for the blog, it was useful for me. I have a file named App_project.mlapp.zip and when I extract it through Winzip it gives all the files exactly as you described above. I...
  • Mr Ashley Trowell (76 days 7 hours ago): Thank you so much for this analysis. Also, I find it somewhat horrifying. The take away seems to be to use && / || and NOT and / or. Thanks a bunch! ~Ashley
  • Matt (81 days 9 hours ago): Late reply I know, but you can call custom shortcuts with alt-#. Hold down Alt to see what number is assigned to the shortcuts you’ve created. Now if there was a way to create a...
Contact us
Undocumented Matlab © 2009 - Yair Altman
Scroll to top