This page was generated from source/plotting.ipynb. Binder badge or

Plotting setup

Test [GCJ+17].

[1]:
import matplotlib.pyplot as plt

Show available fonts

[2]:
# http://jonathansoma.com/lede/data-studio/matplotlib/list-all-fonts-available-in-matplotlib-plus-samples/
# List all fonts available in matplotlib plus samples

import matplotlib.font_manager
from IPython.core.display import HTML

def make_html(fontname):
    return "<p>{font}: <span style='font-family:{font}; font-size: 24px;'>{font}</p>".format(font=fontname)

code = "\n".join([make_html(font) for font in sorted(set([f.name for f in matplotlib.font_manager.fontManager.ttflist]))])

HTML("<div style='column-count: 2;'>{}</div>".format(code))
[2]:

C059: C059

Cantarell: Cantarell

D050000L: D050000L

DejaVu Sans: DejaVu Sans

DejaVu Sans Display: DejaVu Sans Display

DejaVu Sans Mono: DejaVu Sans Mono

DejaVu Serif: DejaVu Serif

DejaVu Serif Display: DejaVu Serif Display

Nimbus Mono PS: Nimbus Mono PS

Nimbus Roman: Nimbus Roman

Nimbus Sans: Nimbus Sans

Nimbus Sans Narrow: Nimbus Sans Narrow

P052: P052

STIXGeneral: STIXGeneral

STIXNonUnicode: STIXNonUnicode

STIXSizeFiveSym: STIXSizeFiveSym

STIXSizeFourSym: STIXSizeFourSym

STIXSizeOneSym: STIXSizeOneSym

STIXSizeThreeSym: STIXSizeThreeSym

STIXSizeTwoSym: STIXSizeTwoSym

Source Code Pro: Source Code Pro

Standard Symbols PS: Standard Symbols PS

URW Bookman: URW Bookman

URW Gothic: URW Gothic

Z003: Z003

cmb10: cmb10

cmex10: cmex10

cmmi10: cmmi10

cmr10: cmr10

cmss10: cmss10

cmsy10: cmsy10

cmtt10: cmtt10

[3]:
# ?matplotlib.font_manager.fontManager.addfont

Add latin modern fonts

[4]:
# https://www.archlinux.org/packages/community/any/otf-latin-modern/
# !sudo pacman -Syu --needed --noconfirm otf-latin-modern inkscape
# !brew cask install font-latin-modern
# fonts_path = "/usr/share/texmf/fonts/opentype/public/lm/" #ubuntu
# fonts_path = "~/Library/Fonts/" # macos
fonts_path = "/usr/share/fonts/OTF/" # arch
matplotlib.font_manager.fontManager.addfont(fonts_path + "lmsans10-regular.otf")
matplotlib.font_manager.fontManager.addfont(fonts_path + "lmroman10-regular.otf")

Set matplotlib to use Latin Modern fonts

[5]:
from IPython.display import set_matplotlib_formats
#%matplotlib inline
set_matplotlib_formats('svg') # use SVG backend to maintain vectorization
plt.style.use('default') #reset default parameters
# https://stackoverflow.com/a/3900167/446907
plt.rcParams.update({'font.size': 16,
                     'font.family': ['sans-serif'],
                     'font.serif': ['Latin Modern Roman'] + plt.rcParams['font.serif'],
                     'font.sans-serif': ['Latin Modern Sans'] + plt.rcParams['font.sans-serif']})
<ipython-input-5-5f8582c2894e>:3: DeprecationWarning: `set_matplotlib_formats` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.set_matplotlib_formats()`
  set_matplotlib_formats('svg') # use SVG backend to maintain vectorization

Check rcParams after update and list fonts available to matplotlib

[6]:
# plt.rcParams.values

# code = "\n".join([make_html(font) for font in sorted(set([f.name for f in matplotlib.font_manager.fontManager.ttflist]))])

# HTML("<div style='column-count: 2;'>{}</div>".format(code))

Create a test plot

[7]:
# import numpy as np

# t = np.arange(-1.0, 2.0, 0.01)
# s = 1 + np.sin(2 * np.pi * t)

# fig, ax = plt.subplots()
# ax.plot(t, s)

# ax.set(xlabel='time (s)', ylabel='voltage (mV)',
#        title='About as simple as it gets, folks')
# ax.grid()

# plt.savefig("fig/test-plot.svg", bbox_inches="tight");
# !inkscape fig/test-plot.svg --export-filename=fig/test-plot.pdf;