Last active
August 9, 2020 03:23
-
-
Save zesty/de6f872908add8aca3c3a156431baa3b to your computer and use it in GitHub Desktop.
More complete view of available fonts in matplotlib (jupyter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# run this in a jupyter notebook; looks much better in jupyter lab | |
# based on: http://jonathansoma.com/lede/data-studio/matplotlib/list-all-fonts-available-in-matplotlib-plus-samples/ | |
import matplotlib.font_manager | |
from IPython.core.display import HTML | |
def make_html(font, fonts): | |
default_size = '26px' | |
html = f"<p>{font}<br>" | |
for f in fonts[font]: | |
shared = f"font-stretch: {f[1]}; font-style: {f[2]}; font-variant: {f[3]}; font-weight: {f[4]}; font-size: {f[5] if f[5] != 'scalable' else default_size}; " | |
style = f"font-family:{f[0]}; {shared}" | |
tooltip = shared.replace('; ', '\n') | |
html += f"<span title='{tooltip}' style='{style}'>THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG the quick brown fox jumps over the lazy dog 0123456789+-=~@#$%^&*_()[]{{}}<>\\|/`'\";:,.!?</span> {f[6]}<br>" | |
html += '</p>' | |
return html | |
def get_fonts(): | |
# 'fname', 'name', 'size', 'stretch', 'style', 'variant', 'weight' | |
# omitting fname, because filename isn't interesting | |
# 0 1 2 3 4 5 6 | |
fonts = [[f.name, f.stretch, f.style, f.variant, str(f.weight), f.size, '(afm)'] for f in matplotlib.font_manager.fontManager.afmlist] + \ | |
[[f.name, f.stretch, f.style, f.variant, str(f.weight), f.size, '(ttf)'] for f in matplotlib.font_manager.fontManager.ttflist] | |
wts = matplotlib.font_manager.weight_dict | |
for i, f in enumerate(fonts): | |
if f[4] in wts.keys(): | |
f[4] = wts[f[4]] | |
fonts[i] = tuple(f) | |
fonts = list(set(fonts)) | |
fonts = [ f for f in fonts if not f[0].lower().startswith('noto') ] # TODO FIXME I'm not a fan of these <----------------- !!!!!!!!!!!!!!!!!!! LOOK !!!!!!!!!!!!!!!!!!!!!!!!!!!! | |
# sort by name, stretch, weight, style | |
fonts.sort(key=lambda x: (x[0] + x[1] + str(x[4]) + x[2]).lower().replace('normal', '000000').replace('regular', '000000')) # sort plain before italic, bold | |
fontdict = { f[0] : [] for f in fonts } | |
for f in fonts: | |
fontdict[f[0]].append(f) | |
return fontdict | |
def get_html(): | |
fonts = get_fonts() | |
html = "\n".join([make_html(font, fonts) for font in fonts.keys()]) | |
return f"<div style='column-count: 1;'>{html}</div>" | |
HTML(get_html()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment