acgc.figstyle
Style settings for matplotlib, for publication-ready figures
Activate the style by importing this module:
from acgc import figstyle
Figures generated by Matplotlib will then use the new style.
The style includes grid lines (horizontal and vertical) by default.
Turn these off and on with grid_off
and grid_on
:
figstyle.grid_off()
figstyle.grid_on()
1#!/usr/bin/env python3 2'''Style settings for matplotlib, for publication-ready figures 3 4Activate the style by importing this module: 5 6`from acgc import figstyle` 7 8Figures generated by Matplotlib will then use the new style. 9The style includes grid lines (horizontal and vertical) by default. 10Turn these off and on with `grid_off` and `grid_on`: 11 12`figstyle.grid_off()` 13 14`figstyle.grid_on()` 15''' 16 17import os 18import warnings 19import matplotlib as mpl 20import matplotlib.style as mstyle 21import matplotlib.font_manager as mfonts 22if 'inline' in mpl.get_backend(): 23 import matplotlib_inline 24 25# Path to this module 26_PATH = os.path.dirname(__file__) 27 28def activate_style(grid=True,gridaxis='both'): 29 '''Activate style sheet 30 31 Parameters 32 ---------- 33 grid : bool, default=True 34 turn grid lines on (True) or off (False) 35 gridaxis : str, default='both' 36 specifies which axes should have grid lines 37 ''' 38 mstyle.use(os.path.join(_PATH,'acgc.mplstyle')) 39 40 # Turn grid on or off 41 if grid: 42 grid_on(axis=gridaxis) 43 44 # Use high quality for inline images; Only use this if the inline backend is active 45 # 'png' is default, 'svg' is also good 46 if 'inline' in mpl.get_backend(): 47 matplotlib_inline.backend_inline.set_matplotlib_formats('retina') 48 49 50def grid_off(): 51 '''Turn off grid lines''' 52 mpl.rcParams['axes.grid'] = False 53 54def grid_on(axis='both'): 55 '''Turn on grid lines 56 57 Parameters 58 ---------- 59 axis : {'both', 'x', 'y'} 60 specifies which axes should have grid lines 61 ''' 62 mpl.rcParams['axes.grid'] = True 63 mpl.rcParams['axes.grid.axis'] = axis 64 65def load_fonts(): 66 '''Load fonts contained in the acgc/fonts subdirectory''' 67 68 # User fonts 69 fonts_pylib = mfonts.findSystemFonts(os.path.join(_PATH,'fonts')) 70 71 # Cached fonts 72 fonts_cached = mfonts.fontManager.ttflist 73 fonts_cached_paths = [ font.fname for font in fonts_cached ] 74 75 # Add fonts that aren't already installed 76 rebuild = False 77 for font in fonts_pylib: 78 if font not in fonts_cached_paths: 79 if rebuild == False: 80 # Issue warning, first time only 81 warnings.warn('Rebuilding font cache. This can take time.') 82 rebuild = True 83 mfonts.fontManager.addfont(font) 84 85 # Save font cache 86 if rebuild: 87 cache = os.path.join( 88 mpl.get_cachedir(), 89 f'fontlist-v{mfonts.FontManager.__version__}.json' 90 ) 91 mfonts.json_dump(mfonts.fontManager, cache) 92 93### 94load_fonts() 95activate_style()
def
activate_style(grid=True, gridaxis='both'):
29def activate_style(grid=True,gridaxis='both'): 30 '''Activate style sheet 31 32 Parameters 33 ---------- 34 grid : bool, default=True 35 turn grid lines on (True) or off (False) 36 gridaxis : str, default='both' 37 specifies which axes should have grid lines 38 ''' 39 mstyle.use(os.path.join(_PATH,'acgc.mplstyle')) 40 41 # Turn grid on or off 42 if grid: 43 grid_on(axis=gridaxis) 44 45 # Use high quality for inline images; Only use this if the inline backend is active 46 # 'png' is default, 'svg' is also good 47 if 'inline' in mpl.get_backend(): 48 matplotlib_inline.backend_inline.set_matplotlib_formats('retina')
Activate style sheet
Parameters
- grid (bool, default=True): turn grid lines on (True) or off (False)
- gridaxis (str, default='both'): specifies which axes should have grid lines
def
grid_off():
Turn off grid lines
def
grid_on(axis='both'):
55def grid_on(axis='both'): 56 '''Turn on grid lines 57 58 Parameters 59 ---------- 60 axis : {'both', 'x', 'y'} 61 specifies which axes should have grid lines 62 ''' 63 mpl.rcParams['axes.grid'] = True 64 mpl.rcParams['axes.grid.axis'] = axis
Turn on grid lines
Parameters
- axis ({'both', 'x', 'y'}): specifies which axes should have grid lines
def
load_fonts():
66def load_fonts(): 67 '''Load fonts contained in the acgc/fonts subdirectory''' 68 69 # User fonts 70 fonts_pylib = mfonts.findSystemFonts(os.path.join(_PATH,'fonts')) 71 72 # Cached fonts 73 fonts_cached = mfonts.fontManager.ttflist 74 fonts_cached_paths = [ font.fname for font in fonts_cached ] 75 76 # Add fonts that aren't already installed 77 rebuild = False 78 for font in fonts_pylib: 79 if font not in fonts_cached_paths: 80 if rebuild == False: 81 # Issue warning, first time only 82 warnings.warn('Rebuilding font cache. This can take time.') 83 rebuild = True 84 mfonts.fontManager.addfont(font) 85 86 # Save font cache 87 if rebuild: 88 cache = os.path.join( 89 mpl.get_cachedir(), 90 f'fontlist-v{mfonts.FontManager.__version__}.json' 91 ) 92 mfonts.json_dump(mfonts.fontManager, cache)
Load fonts contained in the acgc/fonts subdirectory