Upright mu in plot label: preserving original tick fonts

I have a problem that I thought would be more noticeable. However, after cleaning the Internet for some time, I could not find a solution to my problem. So here it is:

For a plot created using matplotlib.pyplot, I want to include the SI-unit micrometer in my xlabel. However, the microcomputer must be upright. After some messing around I reached the desired xlabel.

The code I have to generate 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 micrometer is exactly what I want. However, the problem is that the font of the x and y ticks is changing. This is due to:

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

How can I reset font values ​​to their original values? Or how can I make sure the fonts do not change when typing tex?

By reference, the initial values ​​that I have in mind are as follows: enter image description here In addition to trying to return the fonts back to their original values, I also tried different methods for including a micrometer in my xlabel. The problem that arises here is that it stays italic or bold. The micrometer I'm looking for is the one shown in the first image.

I hope someone helps me solve this problem.

Thanx in advance

+4
source share
3 answers

I am also struggling with such a problem, i.e. so that labels of labels and axes are consistent when text.usetex = True. The solution I managed to find is not perfect, but it works at the moment.

What you need to do is install the font family in "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 define the font family, and I also had to add extra text to the x-tag so that the detection really worked (you can delete this text later and after that the tag will work.

For me, this leads to: enter image description here In general, I have the following ~ / .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} 
+2
source

What worked for me was not for usetex , but for using Unicode:

 ax.set_xlabel(u'\u03bc') 

sets the mark as the only vertical mu.

When loading matplotlib , the following settings are required:

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

Here I use the Sourceforge "Computer Modern Unicode" font (recommended if you want to consistently write entries in LaTeX and its default Computer Modern font).

But any mu glyph unicode font should work. In fact, mu from CMU Serif is not as aesthetic as mu from SIunitx, but it is correct.

You need to restart Python in order for it to take effect.

+1
source

I had the same problem and this solved it:

In your matplotlibrc file change the file

 mathtext.default : it 

to

 mathtext.default : regular 
0
source

Source: https://habr.com/ru/post/971171/


All Articles