Upright mu in plot label: retaining original tick fonts
Asked Answered
P

3

8

I have a problem which I thought would be more occurring. However, after scouring the internet for some time now I have not been able to find the solution to my problem. So here it goes:

For a plot, created using matplotlib.pyplot I want to incorporate the SI-unit micro meter into my xlabel. The unit micro meter however needs to be upright. After some fiddling around I achieved the desired xlabel.

The code that I have to generate this is:

import matplotlib
import matplotlib.pyplot as plt

matplotlib.rc('text', usetex = True)
params = {'text.latex.preamble': [r'\usepackage{siunitx}', r'\usepackage{cmbright}']}
plt.rcParams.update(params)


fig = plt.figure()
ax = fig.add_subplot(1,1,1)

ax.set_xlabel('$\si{\micro \meter}$', fontsize = 16)
ax.set_ylabel("hi", fontsize = 16)

plt.savefig("test.png")

The result is shown below: enter image description here The micro meter is exactly as I want it to be. The problem however is that the font of the x and y ticks is altered. This is because of:

matplotlib.rc('text', usetex = True)

How can I reset the font values to their original values? Or how can I make sure the fonts are not changed when introducing tex?

As a reference, the original values I am referring to look like this: enter image description here Besides trying to revert the fonts back to their original values I also tried different methods of incorporating micro meter into my xlabel. The problem that arises here is that I it remains italic or it has a bold type setting. The micro meter I am looking for is the one given in the first figure.

I hope someone can help me solve this problem.

Thanx in advance

Pismire answered 12/3, 2014 at 10:35 Comment(0)
C
2

I am also struggling with such a problem, i.e. getting the tick labels and axes labels to be consistent when text.usetex = True. The solution that I have managed to find it not ideal, but it works for the moment.

What you have to do is set the font family to "sans-serif" and also add a latex package that uses sans-serif math fonts (sfmath -- make sure it is in your tex path!)

import matplotlib
import matplotlib.pyplot as plt

matplotlib.rc('text', usetex = True)
matplotlib.rc('font', **{'family':"sans-serif"})
params = {'text.latex.preamble': [r'\usepackage{siunitx}', 
    r'\usepackage{sfmath}', r'\sisetup{detect-family = true}',
    r'\usepackage{amsmath}']}   
plt.rcParams.update(params)   

fig = plt.figure(figsize = (4,4))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('$\si{\um} detection$')
ax.set_ylabel(r"$\mu \boldsymbol{\mu}$")
plt.show()                              

In addition, I had to tell the siunitx package to detect the font family, and I also had to add some additional text to the x-label in order for the detection to actually work (you can remove this text later and the label will still work after that).

For me this results in: enter image description here More generally, I have the following my ~/.matplotlib/matploblibrc file, for serif fonts:

font.family    : serif 
text.latex.preamble    :   \usepackage{mathptmx}

and for sans-serif:

font.family : sans-serif 
text.latex.preamble :   \usepackage{sfmath}
Clevis answered 13/3, 2014 at 10:59 Comment(7)
Thanks a lot. Still searching for the "correct" labels but this will definitely do for now. Thanks again!Pismire
By "correct" do you mean the sans-serif ones from your example? I have no idea why, but when I run your example (with all matplotlibrc settings reset) I get serif fonts for the micrometer label (which don't change when I use my serif settings). But since you seem to be able to get sans-serif automatically, what happens if you use the sans-serif settings I suggested?Clevis
I managed to get it working with sans-serif, see my edit to the post!Clevis
Thanks! This brings me one step closer to the desired result! The mu is perfect now! What I was referring to with "correct" is the font family used for "1.0" which changes when using usetex = True.Pismire
Hi, I now would like to use the amsmath package. When I do this in combination with sfmath, amsmath does not respond. When I remove the sfmath package, the ticks of the axis are ugly as it is not sans-serif any more I guess. Any ideas to make the ticks the way they look in your answer in combination with the amsmath package?Pismire
For me it works -- see my edit to the post. Can you be more specific?Clevis
It wouldn't work for me, and I had to install the SIunits latex package as it says the siunitx one is too old. Running win8 over here with python 2.7.Derange
G
3

What worked for me was not to usetex, but to use Unicode:

ax.set_xlabel(u'\u03bc')

sets the label as a single upright mu.

This requires the following settings when loading matplotlib:

import matplotlib
matplotlib.rcParams['mathtext.fontset'] = 'cm'
matplotlib.rc('font', family='serif', serif='CMU Serif')
import matplotlib.pyplot as plt

Here I'm using the "Computer Modern Unicode" font from Sourceforge (highly recommended if you'd like consistency with writing typeset in LaTeX and its default Computer Modern font).

But any unicode font with the mu glyph should work. Actually, the mu from CMU Serif is not as aesthetically nice as the mu from SIunitx, but it is correct.

Python needed to be restarted for that to take effect.

Guideboard answered 3/8, 2017 at 11:4 Comment(0)
C
2

I am also struggling with such a problem, i.e. getting the tick labels and axes labels to be consistent when text.usetex = True. The solution that I have managed to find it not ideal, but it works for the moment.

What you have to do is set the font family to "sans-serif" and also add a latex package that uses sans-serif math fonts (sfmath -- make sure it is in your tex path!)

import matplotlib
import matplotlib.pyplot as plt

matplotlib.rc('text', usetex = True)
matplotlib.rc('font', **{'family':"sans-serif"})
params = {'text.latex.preamble': [r'\usepackage{siunitx}', 
    r'\usepackage{sfmath}', r'\sisetup{detect-family = true}',
    r'\usepackage{amsmath}']}   
plt.rcParams.update(params)   

fig = plt.figure(figsize = (4,4))
ax = fig.add_subplot(1,1,1)
ax.set_xlabel('$\si{\um} detection$')
ax.set_ylabel(r"$\mu \boldsymbol{\mu}$")
plt.show()                              

In addition, I had to tell the siunitx package to detect the font family, and I also had to add some additional text to the x-label in order for the detection to actually work (you can remove this text later and the label will still work after that).

For me this results in: enter image description here More generally, I have the following my ~/.matplotlib/matploblibrc file, for serif fonts:

font.family    : serif 
text.latex.preamble    :   \usepackage{mathptmx}

and for sans-serif:

font.family : sans-serif 
text.latex.preamble :   \usepackage{sfmath}
Clevis answered 13/3, 2014 at 10:59 Comment(7)
Thanks a lot. Still searching for the "correct" labels but this will definitely do for now. Thanks again!Pismire
By "correct" do you mean the sans-serif ones from your example? I have no idea why, but when I run your example (with all matplotlibrc settings reset) I get serif fonts for the micrometer label (which don't change when I use my serif settings). But since you seem to be able to get sans-serif automatically, what happens if you use the sans-serif settings I suggested?Clevis
I managed to get it working with sans-serif, see my edit to the post!Clevis
Thanks! This brings me one step closer to the desired result! The mu is perfect now! What I was referring to with "correct" is the font family used for "1.0" which changes when using usetex = True.Pismire
Hi, I now would like to use the amsmath package. When I do this in combination with sfmath, amsmath does not respond. When I remove the sfmath package, the ticks of the axis are ugly as it is not sans-serif any more I guess. Any ideas to make the ticks the way they look in your answer in combination with the amsmath package?Pismire
For me it works -- see my edit to the post. Can you be more specific?Clevis
It wouldn't work for me, and I had to install the SIunits latex package as it says the siunitx one is too old. Running win8 over here with python 2.7.Derange
G
1

I had the same problem and this solved it:

In your matplotlibrc file change

mathtext.default : it

to

mathtext.default : regular
Guyette answered 13/12, 2017 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.