20

I am making some plots with matplotlib, and I've come across a problem with the TeX rendering. It seems that the mathtext x-height is is a bit smaller than the normal Bitstream Vera Sans. See the following example:

x = linspace(0, 30, 300);
y = 0.5*rand(300)+20/(numpy.power(x-15, 2)+4);
xlabel(r'$\omega$ (rad$\cdot$Hz)');
ylabel(r'Intensity$^2$');
title(r'Why is $mathtext$ so much smaller than normal text?');

Plot of some random data illustrating the issue.

As you can see, it's particularly noticeable with greek letters and numbers. Ideally, I'd be able to define some scaling factor that would just make the math text a bit bigger at each font size. Is there any way to do this simply? I do not want to simply use Computer Modern everywhere. I also do not want to compile a new Tex math font, if that's even possible.

One solution that I would be on board with is using sans-serif fonts for the greek letters and numerals, but for whatever reason, matplotlib ignores formatting on those:

title('Why does $mat\mathsf{plot}lib$ ignore formatting for $\mathsf{2}$ ($\mathsf{two}), $\mathbf{2}$ ($\mathbf{two}) and $\mathsf{\omega}$?')

Title changed

I assume it's something to do with the nature of how these things are typeset, but is there any way to fix it?

2
  • Are using the internal math render or an external latex installation? Commented Apr 5, 2013 at 14:10
  • I think internal math render. I haven't changed it from the default. rcParams['pgf.texsystem'] resolves to 'xelatex', but I don't really know what that's used for. Commented Apr 5, 2013 at 14:11

3 Answers 3

12

From the matplotlib docs:

Additionally, you can use \mathdefault{...} or its alias \mathregular{...} to use the font used for regular text outside of mathtext. There are a number of limitations to this approach, most notably that far fewer symbols will be available, but it can be useful to make math expressions blend well with other text in the plot.

Sign up to request clarification or add additional context in comments.

2 Comments

Beautiful. You can apparently just add rcParams['mathtext.default']='regular', and that sets the text to the normal font. I think I had tried that before but I must have messed up the implementation. That works now.
Looking at the docs is always a good idea
3

Try setting

rcParams['text.usetex'] = True

which will try to use an external LaTeX installation, rather than the built in mathtext renderer. See this wiki (even though it is a tad out-dated) and the discussion on issue #1336

Another option, if you are targeting LaTeX, is to use pstricks.

5 Comments

This is very much not ideal. It sets all text rendering to be done by TeX (something I don't want), which sets the font to be Computer Modern. It does introduce sans-serif numbers, which is nice, but formatting is still ignored on greek letters.
does strait up TeX support the formatting on the greek letters? (That is make a .tex file and see if it renders as you expect there). I suspect you might also be running into issues with font, in that not all fonts contain glyphs for all styles of all symbols.
I can check in a bit, but I'd prefer to use Myriad Pro, which does have glyphs for greek letters, but I don't think it's supported in TeX without me specifically compiling it. I am assuming that the better way to go is to find a way to scale tex fonts when they are used.
Another option, if you are dumping this into an LaTeX document, is to use pstricks to do the formatting later.
Excellent suggestion, actually. In the end, I'm going to use the 'regular' option, because the figures don't go exclusively in Latex files, but good to know about PStricks - since I could always add that into my workflow (have some 'make the fonts look nice' module or something and export to EPS)
0

This problem should be fixed in matplotlib 2. See issue

Different font size between math mode and regular text #7778.

and

Matplotlib: correct math font size.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.