Skip to content

Latest commit

 

History

History
115 lines (92 loc) · 3.23 KB

plotting.md

File metadata and controls

115 lines (92 loc) · 3.23 KB
jupyter
celltoolbar jupytext kernelspec language_info rise toc-autonumbering toc-showcode toc-showmarkdowntxt
Slideshow
cell_metadata_json formats notebook_metadata_filter text_representation
true
ipynb,md,py:percent
all
extension format_name format_version jupytext_version
.md
markdown
1.2
1.9.1
display_name language name
Python 3
python
python3
codemirror_mode file_extension mimetype name nbconvert_exporter pygments_lexer version
name version
ipython
3
.py
text/x-python
python
python
ipython3
3.8.5
scroll theme
true
black
true
false
false

Plotting setup

import matplotlib.pyplot as plt

Show available fonts

# 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))
# ?matplotlib.font_manager.fontManager.addfont

Add latin modern fonts

# 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
# matplotlib.font_manager.fontManager.addfont("~/Library/Fonts/lmsans10-regular.otf")
# matplotlib.font_manager.fontManager.addfont("~/Library/Fonts/lmroman10-regular.otf")
matplotlib.font_manager.fontManager.addfont("/usr/share/fonts/OTF/lmsans10-regular.otf")
matplotlib.font_manager.fontManager.addfont("/usr/share/fonts/OTF/lmroman10-regular.otf")

Set matplotlib to use Latin Modern fonts

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']})

Check rcParams after update and list fonts available to matplotlib

# 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

# 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;