简体   繁体   中英

Greek letters are printed bold in matplotlib latex output

I'm using latex output in matplotlib to print greek symbols for axis and legends. The (serif) text is printed in normal font weight, but some special characters are printed in bold. In particular, I mean the mu for micro, but also the (non-greek) symbol for Angstrom. However, other greek symbols like Omega are printed normally. I made a little example to demonstrate:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import matplotlib as mpl
import matplotlib.pyplot as plt


mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.unicode'] = True

mpl.rcParams['font.family'] = 'serif'
mpl.rcParams['text.latex.preamble'] = [
    r"\usepackage{textgreek}",
    r"\usepackage{siunitx}"]


fig = plt.figure()

plt.plot([1, 2, 4], [0, 9, 7])
plt.xlabel(
    r"\si{\micro\metre} " +
    r"\si{\omega\metre} " +
    r"\si{\nano\metre} " +
    r"\si{\angstrom} " +
    r"\si{\micro\metre}\textmu")
plt.show()

fig.savefig('test.png')
fig.savefig('test.pdf')

I have this issue on live output, vector output and image output. What's confusing for me, is that I used my script for month on the outdate Ubuntu machine on my work, but with python 3.3 installed. Now I tried to run the script on my personal Arch machine and I get the issues.

It seems like it is a problem with the fonts you have installed, in my case I could fix the problem by specifying the font I want to use by the following commands:

mpl.rcParams['font.serif'] = 'Times'

A list of fonts available can be found here: http://matplotlib.org/users/customizing.html

I'm sorry, as Achim mentioned, I should have posted my solution here.

It's nothing special, just my font installation on my Arch is a little bit screwed up. But the Palatino font, which I also use in my documents, is working well, so I made the decision to use it also in Pyplot. This is how my standard font configuration in Pyplot looks like:

import matplotlib as mpl

mpl.rcParams['legend.fontsize'] ="large"
mpl.rcParams['axes.labelsize'] = "x-large"
mpl.rcParams['lines.linewidth'] = 2

mpl.rcParams['xtick.labelsize'] = "x-large"
mpl.rcParams['ytick.labelsize'] = "x-large"

mpl.rcParams['text.usetex'] = True
mpl.rcParams['text.latex.unicode'] = True
mpl.rcParams['text.latex.preamble'] = r"\usepackage{textcomp}" + \
    r"\usepackage{textgreek}" +\
    r"\usepackage{subscript}" +\
    r"\usepackage{siunitx}" +\
    r'\usepackage{amsmath}' +\
    r"\usepackage[osf]{mathpazo}"

mpl.rcParams['font.family'] = "Palatino"

One short remark: I'm using old style figures within the mathpazo package. This isn't very useful for equations and scientific numbers. If you also want to use old style figures, I really suggest you to use the SIunitx packages (I suggest it anyway), where osf are avoided.

If you want to avoid osf for a free standing number, simply use \\num{123}, or together with a unit \\SI{123}{\\celsius\\per\\square\\hour}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM