Skip to content

Instantly share code, notes, and snippets.

@baileythegreen
Created May 20, 2021 23:49
Show Gist options
  • Save baileythegreen/7c9809a35157ed4f96aee776a96c707d to your computer and use it in GitHub Desktop.
Save baileythegreen/7c9809a35157ed4f96aee776a96c707d to your computer and use it in GitHub Desktop.
Customising font options in matplotlib and Seaborn.
import sys, os, optparse
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.lines import Line2D
# may be necessary if you have to add fonts to the path (probably only applies if you've installed new ones)
matplotlib.font_manager._rebuild()
import matplotlib.font_manager
fe = matplotlib.font_manager.FontEntry(
fname='/Users/baileythegreen/Library/Fonts/',
name='Nordik')
matplotlib.font_manager.fontManager.ttflist.insert(0, fe) # or append is fine
matplotlib.rcParams['font.family'] = fe.name # = 'your custom ttf font name'
# A different method for the same thing?
#matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
#font_dirs = ['/Users/baileythegreen/Library/Fonts/', ]
#font_files = matplotlib.font_manager.findSystemFonts(fontpaths=font_dirs)
#font_list = matplotlib.font_manager.createFontList(font_files)
#matplotlib.font_manager.fontManager.ttflist.extend(font_list)
matplotlib.rcParams['font.family'] = 'Nordik'
# I matched the colours to my text colour, which is not black
COLOR = "#3A4E7E"
matplotlib.rcParams['text.color'] = COLOR
matplotlib.rcParams['axes.labelcolor'] = COLOR
matplotlib.rcParams['xtick.color'] = COLOR
matplotlib.rcParams['ytick.color'] = COLOR
#matplotlib.rcParams['fontname'] = '/Users/baileythegreen/Library/Fonts/Nordik.ttf'
# Load in some data, here represented by the variable 'data'
a4_dims = (11.75, 9)
fig, ax = plt.subplots(figsize=a4_dims)
#hist = sns.histplot(data, x = "data", hue = "sex", hatch = "/", palette = {'female': "#0080FF", 'male': "#800080"}, multiple = "layer", bins = 100, linewidth=.5, stat = "count", edgecolor = "none")
hist = sns.histplot(data[data["sex"] == "female"], x = "data", element = "step", color = "#0080FF", ax = ax, edgecolor = "none", bins = "auto", linewidth=.5, stat = "count", alpha = .5)
hist = sns.histplot(data[data["sex"] == "male"], x = "data", element = "step", hatch = "+", ax = ax, edgecolor = "#6A459F", color = "#6A459F", bins = "auto", linewidth=.5, stat = "count", alpha = .7)
sns.despine(fig)
#hist.set(xlabel = "%s minor allele frequencies" % opts.cohort_name)
plt.xlabel(xlabel = "x-axis label", fontsize = 18, fontname = 'Nordik', fontstyle = 'normal', fontweight = "regular")
plt.ylabel("Count", fontsize = 18, fontname = 'Nordik', fontstyle = 'normal')
plt.ticklabel_format(style = 'plain', axis = 'y')
a = Line2D([], [], color = "#0080FF", label = "Female", marker = "o")
b = Line2D([], [], color = "#6A459F", label = "Male", marker = "o")
plt.legend(handles = (a, b), loc = "upper right", fontsize = 18, title_fontsize = "18")
plt.tick_params(axis = "x", labelsize = 14, bottom = False, top = False)
plt.tick_params(axis = "y", labelsize = 14, left = False, right = False)
@baileythegreen
Copy link
Author

NB. Not all of the lines at the top are required, but I am not sure which ones are optional. I just know this combination worked.

I have left the plot code, even though it was for a specific thing, because it also shows how you can specify font in the different commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment