I downloaded fonts from Google Fonts which I want to use for my thesis and all plots, namely the Alegreya font family. After installing the fonts to $HOME/.fonts I removed .cache/matplotlib/fontList.cache and created a plot with the new font.family = 'Alegreya Sans' in the rcParams. Unfortunately, matplotlib detects the Thin font of Alegreya Sans as the Regular one. The false detection is illustrated by the following minimal example:
import matplotlib.font_manager
weights = ['ultralight', 'light', 'normal', 'regular', 'book', 'medium',
'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy',
'extra bold', 'black']
print 'weight'+6*' ', 'file name', '\n'+70*'-'
for weight in weights:
fprops = matplotlib.font_manager.FontProperties(family='Alegreya Sans',
weight=weight)
print weight+(12-len(weight))*' ', matplotlib.font_manager.findfont(fprops)
Output:
weight file name
----------------------------------------------------------------------
ultralight /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-Light.ttf
light /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-Light.ttf
normal /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-Thin.ttf
regular /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-Thin.ttf
book /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-Thin.ttf
medium /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-Medium.ttf
roman /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-Medium.ttf
semibold /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-ExtraBold.ttf
demibold /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-ExtraBold.ttf
demi /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-ExtraBold.ttf
bold /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-ExtraBold.ttf
heavy /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-ExtraBold.ttf
extra bold /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-ExtraBold.ttf
black /hmi/kme/.fonts/Alegreya_Sans/AlegreyaSans-Black.ttf
How can this be fixed and why does it happen? Thanks in advance!
Of course, there are more files in the folder /hmi/kme/.fonts/Alegreya_Sans/
A complete list:
I fixed the problem manually with a work-around. Anyhow this seems to be a bug. As tom mentioned, I copied all fonts to $HOME/.fonts without subdirectories. This changed the output from the script I gave in the question. To find out what's wrong I tried to print all combinations of weight and style of Alegreya and Alegreya Sans in the following way:
import matplotlib
matplotlib.use('Qt4Agg')
print matplotlib.get_backend()
import matplotlib.pyplot as plt
alignment = {'horizontalalignment':'center', 'verticalalignment':'baseline'}
alegreya_weights = { 'Alegreya': ['regular', 'bold', 'black'],
'Alegreya Sans': ['ultralight', 'light', 'regular',
'medium', 'bold', 'extra bold',
'black'] }
styles = ['normal', 'italic']
combinations = []
for family in alegreya_weights.keys():
for style in styles:
for weight in alegreya_weights[family]:
combinations.append((family, weight, style))
N = len(combinations)
def textPlot(ax, i, N, family, weight, style):
y = 1.-(1./float(N)) -float(i)/(float(N)+1)
ax.text(0.5, y, family+' '+weight+' '+style,
family=family, weight=weight, style = style,
fontsize = 30, **alignment)
fig = plt.figure(figsize=(8, .7*N), frameon=False)
ax = plt.gca()
ax.axis('off')
plt.xlim((0.,1.))
plt.ylim((0.,1.))
for i,c in enumerate(combinations):
textPlot(ax, i, N, c[0], c[1], c[2])
plt.show()
It turned out that only AlegreyaSans-Regular.ttf was replaced by AlegreyaSans-Thin.ttf. I couldn't find out if this is due to the word "Thin", which is referred to as "ultralight" in matplotlib-syntax. I replaced "AlegreyaSans-Thin.ttf" by "AlegreyaSans-Regular.ttf" in $HOME/.cache/matplotlib/fontList.cache to solved the problem.
Now the following image results from the script above. Thin (ultralight) is not detected.
Thanks to tom for any help.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments