Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context styling #138

Merged
merged 3 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 29 additions & 68 deletions ctaplot/plots/style.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import matplotlib as mpl
from distutils.spawn import find_executable
import warnings

_SizeTitlePaper = 11
_SizeLabelPaper = 9
_SizeTickPaper = 8
_SizeTitleSlides = 28
_SizeLabelSlides = 24
_SizeTickSlides = 20

_global_style = 'notebook' # internal - set by `set_style`
from contextlib import contextmanager
from ..io.dataset import get


def check_latex():
Expand All @@ -18,84 +10,53 @@ def check_latex():

Returns
-------
bool
bool: True if a LaTeX distribution could be found
"""
return not find_executable('latex') is None


def set_style(style='notebook'):
@contextmanager
def context(style='notebook'):
"""
Set styling for plots
'slides' and 'paper' require a LaTeX distribution to be installed on the system.
Context manager for styling options
Styling adapted from the `seaborn-deep` style.
'slides' and 'paper' will use the LaTeX distribution if one is available

Parameters
----------
style: str
'notebook', 'slides' or 'paper'
"""
mpl.pyplot.style.use('seaborn-deep')
set_figsize()
_global_style = style
set_font(style=style)


def set_figsize(style='notebook'):
"""
Set default figsize
Parameters
----------
style: str
'notebook', 'slides' or 'paper'
Example
-------
>>> import matplotlib.pyplot as plt
>>> from ctaplot.plots.style import context
>>> with context('notebook'):
>>> plt.plot([1, 2, 4])
"""
if style == 'notebook' or 'slides':
mpl.rcParams['figure.figsize'] = (12, 8)
elif style == 'paper':
mpl.rcParams['figure.figsize'] = (5.25, 3.5) # column-width in inches of a 2-columns article
else:
raise ValueError
style_path = get(f'ctaplot-{style}')
with mpl.style.context(['seaborn-deep', style_path]):
if not check_latex():
mpl.rcParams['text.usetex'] = False
yield


def set_font(style='notebook'):
def set_style(style='notebook'):
"""
Set font style.
'slides' and 'paper' require a LaTeX distribution to be installed on the system.
Set styling for plots adapted from the `seaborn-deep` style.
'slides' and 'paper' will use the LaTeX distribution if one is available

Parameters
----------
output: str
style: str
'notebook', 'slides' or 'paper'
"""
if (style == 'paper' or style == 'slides') and not check_latex():
warnings.warn(f'A LaTeX distribution must be installed to use the {style} style. Switching to notebook style')
style = 'notebook'
mpl.rcParams.update(mpl.rcParamsDefault)

style_path = get(f'ctaplot-{style}')
mpl.pyplot.style.use(['seaborn-deep', style_path])

if style == 'slides' or 'notebook':
size_label = _SizeLabelSlides
size_tick = _SizeTickSlides
size_title = _SizeTitleSlides
if style == 'slides':
mpl.rc('text', usetex=True)
else:
mpl.rc('text', usetex=False)
elif style == 'paper':
size_label = _SizeLabelPaper
size_tick = _SizeTickPaper
size_title = _SizeTitlePaper
mpl.rc('text', usetex=True)
else:
raise ValueError
if not check_latex():
mpl.rcParams['text.usetex'] = False

params = {
'axes.labelsize': size_label,
'axes.titlesize': size_label,
'figure.titlesize': size_title,
'xtick.labelsize': size_tick,
'ytick.labelsize': size_tick,
'legend.fontsize': size_label,
'legend.title_fontsize': size_title,
}
mpl.pyplot.rcParams.update(params)
mpl.rc('font', **{'size': size_label})
mpl.rcParams['mathtext.fontset'] = 'cm'
mpl.rcParams['font.family'] = 'STIXGeneral'

4 changes: 4 additions & 0 deletions ctaplot/plots/tests/test_style.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from ctaplot.plots import style
import matplotlib.pyplot as plt

def test_set_style():
style.set_style('slides')
style.set_style('paper')

def test_context():
with style.context('slides'):
plt.plot([1, 2, 6])
381 changes: 183 additions & 198 deletions examples/notebooks/style.ipynb

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions share/styles/ctaplot-notebook
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#### MATPLOTLIBRC FORMAT


## figure ##

figure.dpi : 200


## font ##

text.usetex : False
mathtext.fontset : cm
font.family : STIXGeneral

24 changes: 24 additions & 0 deletions share/styles/ctaplot-paper
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#### MATPLOTLIBRC FORMAT


## figure ##

# column-width in inches of a 2-columns article
figure.figsize : 5.25, 3.5
figure.dpi : 200
figure.titlesize : 11

## axes ##

axes.titlesize : 11
axes.labelsize : 9
legend.fontsize : 9

## font ##

text.usetex : True
mathtext.fontset : cm
font.family : STIXGeneral



23 changes: 23 additions & 0 deletions share/styles/ctaplot-slides
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#### MATPLOTLIBRC FORMAT


## figure ##

figure.dpi : 200
figure.titlesize : 12

## axes ##

axes.titlesize : 19
axes.labelsize : 15
legend.fontsize : 14

## font ##

text.usetex : True
mathtext.fontset : cm
font.family : STIXGeneral