2015-10-11 6 views
8

Ich verwende Roboto Condensed Schriftart, die ich auf meinen Laptop heruntergeladen habe, für Zahlen mit Matplotlib geplottet. Ich frage mich, ob es möglich ist, die Schriftart "on the fly" wie CSS @ import von Google Fonts zu importieren und sie einfach mit matplotlib zu verwenden.Kann ich Google-Schriftarten mit Matplotlib und Jupyter laden?

Ich benutze Jupyter Notebook für Python. Vielleicht gibt es einen Weg dahin?

Best, F.

+0

https : //stackoverflow.com/questions/7726852/how-to-use-a-random-otf-o--ttf-font-in-matplotlib – Oz123

+0

Was meinst du mit "on the fly"? Möchten Sie den Download überspringen und direkt von Google laden? – Shovalt

Antwort

0

Sie können .ttf Dateien aus dem Repo auf github 'Fonts' Google. Sie können dort eine Schriftart aus der Liste auswählen und einen Link zur .ttf-Datei finden. Wenn Sie beispielsweise in das Verzeichnis "Gleich" wechseln, finden Sie eine Datei mit dem Namen "Alike-Regular.ttf", deren URL lautet: https://github.com/google/fonts/blob/master/ofl/alike/Alike-Regular.ttf.

Sobald Sie Ihre Schrift finden, können Sie das folgende Snippet verwenden es in matplotlib geladen werden "on the fly", eine temporäre Datei mit:

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() 

Ergebnis:

Plot with custom google font

Verwandte Themen