2

So, I would like to use some custom fonts for my graphs in matplotlib. I was able to use the fonts following this post.

import matplotlib as mpl
import matplotlib.font_manager as fm

path_font = 'D:\\myfonts\\'
font_dirs = [path_font + 'CustomSansPro\\', ]
font_files = fm.findSystemFonts(fontpaths=font_dirs)
font_list = fm.createFontList(font_files)
fm.fontManager.ttflist.extend(font_list)
mpl.rcParams['font.family'] = 'Custom Sans Pro'

However, in my custom fonts folder there are several types of font.
If I do:

font_list:

I get something like:

[<Font 'Custom Sans Pro' (CustomSansPro-Bold.ttf) normal normal 700 normal>,
<Font 'Custom Sans Pro' (CustomSansPro-Thin.ttf) normal normal 400  normal>,
<Font 'Custom Sans Pro' (CustomSansPro-LightItalic.ttf) italic normal 200 normal>,
<Font 'Custom Sans Pro' (CustomSansPro-BoldItalic.ttf) italic normal 700 normal>,
<Font 'Custom Sans Pro' (CustomSansPro-Medium.ttf) normal normal 500 normal>,
<Font 'Custom Sans Pro' (CustomSansPro-Light.ttf) normal normal 200 normal>,
...]

At present it I can just use one font type per font folder if I want to be sure that I am using a particular .tff. For example I would like to switch from Light.tff to LightItalic.tff to BoldItalic.tff.

How do I switch from one type to the other in my font list as they have all the same name (but different properties)?

Setting font.serif does not seem to have an effect.

esperluette
  • 1,792
  • 2
  • 14
  • 21
  • Can you `print(font_dirs)` and post the output? – rustyshackleford Oct 16 '17 at 14:34
  • just the directory where my fonts are e.g. ['D:\\myfonts\\CustomSansPro\\'] – esperluette Oct 16 '17 at 14:37
  • Try changing `font_dirs = [path_font + 'CustomSansPro\\', ]` to `font_dirs = path_font + 'CustomSansPro\\'` – rustyshackleford Oct 16 '17 at 14:43
  • I tried, it does not to change anything. What I would like to do is to select the Bold or Thin types for example. As a workaround I could put each font in a separate folder but I think there is a better way of doing it! – esperluette Oct 16 '17 at 14:47
  • From the question it is not really clear whether you actually can use the `Custom Sans Pro` font, when importing it like you do. If this is the case, you may probably just need to additionally set something like `mpl.rcParams['font.weight'] = 'bold'` to get bold font and likewise for the other weights. – ImportanceOfBeingErnest Oct 16 '17 at 19:35
  • Yes, I can use it. But to be sure about which one I am using I need to have only one font file in the font folder (e.g. the file CustomSansPro-BoldItalic.ttf). I would like to keep all CustomSansPro fonts file in one folder and switch from 'Bold', 'Thin', etc. I tried the font.weight command but only 'normal' and 'bold' work. – esperluette Oct 17 '17 at 08:01

0 Answers0