Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing figure fonts in Matlab has no effect

I'm having problems with rendering Greek characters in my labels and legends, so I thought maybe switching fonts would help. Thus, I discovered an even bigger problem - Matlab doesn't seem to honor my font settings at all.

I've tried, of course,

set(gcf, 'FontName', 'Ubuntu')
set(gca, 'FontName', 'Ubuntu')

but also things like

set(findall(gca, 'type', 'text'), 'FontName', 'Ubuntu')

and

set(0,'DefaultAxesFontName', 'Ubuntu')

I also tried switching renderers, with

set(0, 'DefaultFigureRenderer', 'OpenGL') % also tried 'zbuffer' and 'painters' 

I even went into property editor on the figure, and manually changed the fonts. Regardless of what I do, the font comes out with the same, ugly monospaced font (see figure below). This picture is saved with the Save As... command in the menu bar of the figure, but e.g. print -dpng 'fonttest.png' doesn't work either.

I've double-checked both with fc-list in my terminal, and listfonts at the Matlab command prompt, that the Ubuntu font is installed and available. I'm running Matlab R2012b.

How do I set the fonts in my figures?

UPDATE:
As suggested in comments, I tried to see if this is might be an issue with Java on Ubuntu. Sure enough, switching from OpenJDK to Oracle Java helped, but not enough - I can use Helvetica and a few others, but I can't choose freely among my installed fonts. The Ubuntu font, for example, does not work. (It does, however, in the main GUI of Matlab - there I've changed the fonts to Ubuntu in menus etc, and Consolas in the editor and command window, and it renders OK.)

Whenever I choose a figure font that doesn't work, Matlab uses what's shown in the screenshot below as fallback.

enter image description here

The p, q and f here should really be the greek letters pi, theta and phi (they're specified with \pi etc) but I guess the font doesn't have those characters. The weird stuff in the y-label of the third plot is a Swedish ö - it shouldn't be a problem with encodings, because the file is saved as UTF-8, which is also what slCharacterEncoding returns, so I assume that's also a font issue.

like image 816
Tomas Aschan Avatar asked Apr 25 '13 15:04

Tomas Aschan


People also ask

How do I change the font of a MATLAB figure?

To change the font size, set the “FontSize” property for the axes. Since many plotting functions reset axes properties, including the font size, set the "FontSize" property after plotting. For example, the code below sets the font size to 16 points. The tick labels use the specified font size.

What font is used in MATLAB figures?

There is no specific default font for MATLAB. MATLAB chooses a font to display based on your Java settings.

How do I change the default font size in MATLAB?

To set the default font and fontsize, go to File -> Preferences in MATLAB. Within the "Preferences" window, expand the "General" branch at the upper left side of the window and click on the "Font & Colors" branch. Under the "Desktop font" box on the right side of the window, the font, type and fontsize can be changed.


2 Answers

Installing xfonts-75dpi and xfonts-100dpi did the trick for me for the default settings.

sudo apt-get install xfonts-75dpi xfonts-100dpi

and then log out and log in again.

This seems to allow both symbols (e.g. \pi) and font size (e.g. set(h, 'FontSize', 14)) changes (using the default Matlab font).

Machine setup: Ubuntu 12.04 x64 with Matlab R2012b

like image 129
benj Avatar answered Sep 30 '22 16:09

benj


Try using the option 'Interpreter', 'LaTex' (default is 'Interpreter', 'Tex'). This may get you to a less buggy font renderer.

like image 43
Marc Avatar answered Sep 30 '22 18:09

Marc