Can I download Google fonts using Matplotlib and Jupyter?

I use the Roboto Condensed font that I downloaded on my laptop for numbers built using matplotlib. I am wondering if it is possible to import a font on the fly, such as CSS @import, from Google Fonts and use it directly with matplotlib.

I am using Jupyter for python. Maybe there is a way?

Best, F.

+8
matplotlib ipython-notebook jupyter google-font-api
source share
1 answer

You can get .ttf files from google font repository on github . You can select a font from the list and find the link to the .ttf file. For example, if you go to the "alike" directory, you will find a file called "Alike-Regular.ttf" whose URL is: https://github.com/google/fonts/blob/master/ofl/alike/Alike-Regular .ttf .

Once you find your font, you can use the following snippet to upload it to matplotlib on the fly using a temporary file:

from tempfile import NamedTemporaryFile import urllib2 import matplotlib.font_manager as fm import matplotlib.pyplot as plt github_url = 'https://github.com/google/fonts/blob/master/ofl/alike/Alike-Regular.ttf' url = github_url + '?raw=true' # You want the actual file, not some html response = urllib2.urlopen(url) f = NamedTemporaryFile(delete=False, suffix='.ttf') f.write(response.read()) f.close() fig, ax = plt.subplots() ax.plot([1, 2, 3]) prop = fm.FontProperties(fname=f.name) ax.set_title('this is a special font:\n%s' % github_url, fontproperties=prop) ax.set_xlabel('This is the default font') plt.show() 

Result:

Custom Google Font Story

0
source share

All Articles