Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
Problem
The font family "Computer Modern" is commonly used in LaTeX documents.
Even though, in Matplotlib, it is easy to use "Computer Modern Math" for math-text throughplt.rcParams['mathtext.fontstyle'] = 'cm'
, it is unnecessarily complicated to use Computer Modern for non-math-text.
Despite it being listed as the 3rd font in thedefault matplotlibrc file underfont.serif
,font.sans-serif
andfont.monospace
.
One option is compiling the plot through an actual TeX engine, i.e. usingplt.rcParams['text.usetex'] = True
.
However, many people do not have LaTeX installed locally (because ofOverleaf) and it takes up a lot of space.
Additionally, it is way slower than native Matplotlib rendering and thus a bad option if you want to generate a lot of plots.
Another suggestion I have found online is to install the font family in your operating system and useplt.rcParams['font.serif'] = 'Computer Modern Roman'
.
I have found this to not be as straightforward to do because you have to find a good download, then there are so many different files to choose from, and the font might still be not found by Matplotlib after installing it.
After digging through Google and thematplotlib directories, the easiest way to use Computer Modern, in my opinion, are the following rc-parameter settings:
plt.rcParams['font.family']='serif'# or 'sans-serif' or 'monospace'plt.rcParams['font.serif']='cmr10'plt.rcParams['font.sans-serif']='cmss10'plt.rcParams['font.monospace']='cmtt10'plt.rcParams["axes.formatter.use_mathtext"]=True# to fix the minus signs
Furthermore, in my use case, I want to also be able to edit the plots afterwards in Inkscape, so I installed theTTF font files in my operating system and I use
plt.rcParams["pdf.fonttype"]=42plt.rcParams["svg.fonttype"]='none'
However, every time I save a PDF, I get the annoyingfonttools warnings'modified' ...
and'created' timestamp seems very low; regarding as unix timestamp
.
This is caused by the TTF files having the wrong dateFri Jan 01 1904 01:00:00 GMT+0100
in their "Font Header Table" metadata. (I usedfontdrop.info)
Proposed solution
Since Computer Modern is already available to use in Matplotlib, why doesn't
plt.rcParams['font.serif']='Computer Modern Roman'plt.rcParams['font.sans-serif']='Computer Modern Sans Serif'plt.rcParams['font.monospace']='Computer Modern Typewriter'
just work out of the box, as thedefault matplotlibrc file suggests?
Maybe there is a way to make Matplotlib recognise the already available font files (cmr10 cmss10 cmtt10) under those names?
Alternatively, if it is impossible to make those names available out of the box, it might be good to list those font names somewhere in the documentation or the matplotlibrc.
I don't know enough about fonts to suggest a way to fix the Unicode minus sign, but thecmr10 minus sign warning already helps to find this workaround and something similar could be implemented if a font named "Computer Modern" is not found by Matplotlib.
Might it be possible to update the Computer Modern TTF files, so that the mentioned fonttools warning does not appear anymore? Those files were apparently added18 years ago and never touched again. :D
Finally, a lot of people prefer to use the font familyLatin Modern (through thelmodern CTAN package), which is just an improved version of Computer Modern and it also has a maths font.
Do you think it might be a good idea to maybe switch from Computer Modern to Latin Modern in Matplotlib?