How can I fix matplotlib detecting wrong font weights?

physicsPyUser

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!

Edit:

Of course, there are more files in the folder /hmi/kme/.fonts/Alegreya_Sans/

A complete list:

  • AlegreyaSans-BlackItalic.ttf
  • AlegreyaSans-BoldItalic.ttf
  • AlegreyaSans-ExtraBoldItalic.ttf
  • AlegreyaSans-Italic.ttf
  • AlegreyaSans-Light.ttf
  • AlegreyaSans-Medium.ttf
  • AlegreyaSans-ThinItalic.ttf
  • AlegreyaSans-Black.ttf
  • AlegreyaSans-Bold.ttf
  • AlegreyaSans-ExtraBold.ttf
  • AlegreyaSans-LightItalic.ttf
  • AlegreyaSans-MediumItalic.ttf
  • AlegreyaSans-Regular.ttf
  • AlegreyaSans-Thin.ttf

My System

  • OpenSuse 13.1
  • python 2.7
  • matplotlib version 1.4.3
physicsPyUser

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.

enter image description here

Thanks to tom for any help.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I fix matplotlib detecting wrong font weights?

From Dev

How can i get list of font family(or Name of Font) in matplotlib

From Dev

How can i get list of font family(or Name of Font) in matplotlib

From Dev

Why did I get the wrong output and how can I fix this?

From Dev

How can I have a WPF TextBlock that has the same size for all font weights?

From Dev

How can I find web-fonts with full range of font-weights?

From Dev

How can I fix setOnItemClickListener for 2 gridviews that make the wrong result?

From Dev

How can I fix lines broken in wrong places?

From Dev

How can I check the default font of matplotlib in the python shell?

From Dev

Python/Pybrain: How can I fix weights of a neural network during training?

From Dev

Can I mix font weights in the same paragraph when using pdfkit?

From Dev

How do I alias one of the weights of a font family with fontconfig?

From Dev

How can I constrain weights of linear classifier?

From Dev

How can I set widths with weights in Flutter?

From Dev

How can I bootstrap with weights in "randomForestSRC"?

From Dev

How can i fix this?

From Dev

Error running rails server: can you guess what's wrong? How can I fix it?

From Dev

Chrome & Firefox not rendering bold font-weights of Avenir, but Safari does! How to fix?

From Dev

How can i fix my website navigation font from changing across pages

From Dev

Why is cider finding the wrong version of cider-nrepl and how can I fix it?

From Dev

How can I fix my migrations? ActiveRecord::Migrator.current_version and db:migrate:status are wrong

From Dev

Understanding what went wrong with my git merge, and how can I fix it?

From Dev

How can I fix the date formats which have gone wrong in 18.04 when setting first day of the week?

From Dev

How can I use this font?

From Java

How can I fix this CrashlyticsMissingDependencyException?

From Dev

How I can fix element

From Dev

How I can fix this on UnityDecoration?

From Dev

How I can fix this array

From Dev

How can I fix this error?

Related Related

  1. 1

    How can I fix matplotlib detecting wrong font weights?

  2. 2

    How can i get list of font family(or Name of Font) in matplotlib

  3. 3

    How can i get list of font family(or Name of Font) in matplotlib

  4. 4

    Why did I get the wrong output and how can I fix this?

  5. 5

    How can I have a WPF TextBlock that has the same size for all font weights?

  6. 6

    How can I find web-fonts with full range of font-weights?

  7. 7

    How can I fix setOnItemClickListener for 2 gridviews that make the wrong result?

  8. 8

    How can I fix lines broken in wrong places?

  9. 9

    How can I check the default font of matplotlib in the python shell?

  10. 10

    Python/Pybrain: How can I fix weights of a neural network during training?

  11. 11

    Can I mix font weights in the same paragraph when using pdfkit?

  12. 12

    How do I alias one of the weights of a font family with fontconfig?

  13. 13

    How can I constrain weights of linear classifier?

  14. 14

    How can I set widths with weights in Flutter?

  15. 15

    How can I bootstrap with weights in "randomForestSRC"?

  16. 16

    How can i fix this?

  17. 17

    Error running rails server: can you guess what's wrong? How can I fix it?

  18. 18

    Chrome & Firefox not rendering bold font-weights of Avenir, but Safari does! How to fix?

  19. 19

    How can i fix my website navigation font from changing across pages

  20. 20

    Why is cider finding the wrong version of cider-nrepl and how can I fix it?

  21. 21

    How can I fix my migrations? ActiveRecord::Migrator.current_version and db:migrate:status are wrong

  22. 22

    Understanding what went wrong with my git merge, and how can I fix it?

  23. 23

    How can I fix the date formats which have gone wrong in 18.04 when setting first day of the week?

  24. 24

    How can I use this font?

  25. 25

    How can I fix this CrashlyticsMissingDependencyException?

  26. 26

    How I can fix element

  27. 27

    How I can fix this on UnityDecoration?

  28. 28

    How I can fix this array

  29. 29

    How can I fix this error?

HotTag

Archive