Jupyter Snippet CB2nd 01_styles
Jupyter Snippet CB2nd 01_styles
6.1. Using matplotlib styles
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
sorted(mpl.style.available)
['bmh',
'classic',
'dark_background',
'fivethirtyeight',
'ggplot',
'grayscale',
'mycustomstyle',
'seaborn',
...
'seaborn-whitegrid']
def doplot():
fig, ax = plt.subplots(1, 1, figsize=(5, 5))
t = np.linspace(-2 * np.pi, 2 * np.pi, 1000)
x = np.linspace(0, 14, 100)
for i in range(1, 7):
ax.plot(x, np.sin(x + i * .5) * (7 - i))
return ax
mpl.style.use('fivethirtyeight')
doplot()
# Set the default style.
mpl.style.use('default')
# Temporarily switch to the ggplot style.
with mpl.style.context('ggplot'):
ax = doplot()
ax.set_title('ggplot style')
# Back to the default style.
ax = doplot()
ax.set_title('default style')
cfgdir = matplotlib.get_configdir()
cfgdir
'/home/cyrille/.config/matplotlib'
from pathlib import Path
p = Path(cfgdir)
stylelib = (p / 'stylelib')
stylelib.mkdir(exist_ok=True)
path = stylelib / 'mycustomstyle.mplstyle'
path.write_text('''
axes.facecolor : f0f0f0
font.family : serif
lines.linewidth : 5
xtick.labelsize : 24
ytick.labelsize : 24
''')
mpl.style.reload_library()
with mpl.style.context(['ggplot', 'mycustomstyle']):
doplot()