notebooks/common.py
def plot_finalise(ax, title=None, xlabel=None, ylabel=None, 
                  lfooter=None, rfooter='marktheballot.blogspot.com',
                  location='../charts/', close=True, concise_dates=False,
                  straighten_tl=False, **kwargs):
    """Complete and save a plot image"""
    
    # annotate the plot
    if title is not None:
        ax.set_title(title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    
    if lfooter is not None:
        ax.figure.text(0.005, 0.005, lfooter, 
                       ha='left', va='bottom',
                       c='#999999', style='italic', 
                       fontsize=9)

    if rfooter is not None:
        ax.figure.text(0.995, 0.005, rfooter, 
                       ha='right', va='bottom',
                       c='#999999', style='italic', 
                       fontsize=9)
        
    # format dates
    if concise_dates:
        ax.xaxis.set_major_formatter(mdates.ConciseDateFormatter(
            ax.xaxis.get_major_locator()))
    
    # straigten tick labels
    if straighten_tl:
        for tick in ax.get_xticklabels():
            tick.set_rotation(0)
            tick.set_ha('center')
    
    ax.figure.tight_layout(pad=1.1)    
    
    if title is not None:
        replaceable = list(r'/\^:$')
        for char in replaceable:
            title = title.replace(char, '')
        file_stem = f'{location}{title}'
        if 'save_suffix' in kwargs:
            file_stem += '-' + kwargs['save_suffix']
        pathlib.Path(location).mkdir(parents=True, exist_ok=True)
        ax.figure.savefig(f'{file_stem}.png', dpi=300)
    
    # close
    if close:
        plt.show()
        plt.close()
Complete and save a plot image
Complete and save a plot image
Complete and save a plot image
def plot_finalise(ax, title=None, xlabel=None, ylabel=None, 
                  lfooter=None, rfooter='marktheballot.blogspot.com',
                  location='../charts/', close=True, concise_dates=False,
                  straighten_tl=False, **kwargs):
    if title is not None:
        ax.set_title(title)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    if lfooter is not None:
        ax.figure.text(0.005, 0.005, lfooter, 
                       ha='left', va='bottom',
                       c='#999999', style='italic', 
                       fontsize=9)
    if rfooter is not None:
        ax.figure.text(0.995, 0.005, rfooter, 
                       ha='right', va='bottom',
                       c='#999999', style='italic', 
                       fontsize=9)
    if concise_dates:
        ax.xaxis.set_major_formatter(mdates.ConciseDateFormatter(
            ax.xaxis.get_major_locator()))
    if straighten_tl:
        for tick in ax.get_xticklabels():
            tick.set_rotation(0)
            tick.set_ha('center')
    ax.figure.tight_layout(pad=1.1)    
    if title is not None:
        replaceable = list(r'/\^:$')
        for char in replaceable:
            title = title.replace(char, '')
        file_stem = f'{location}{title}'
        if 'save_suffix' in kwargs:
            file_stem += '-' + kwargs['save_suffix']
        pathlib.Path(location).mkdir(parents=True, exist_ok=True)
        ax.figure.savefig(f'{file_stem}.png', dpi=300)
    if close:
        plt.show()
        plt.close()
def plot_finalise ( ax , title = None , xlabel = None , ylabel = None , lfooter = None , rfooter = 'marktheballot.blogspot.com' , location = '../charts/' , close = True , concise_dates = False , straighten_tl = False , ** kwargs ) : if title is not None : ax . set_title ( title ) ax . set_xlabel ( xlabel ) ax . set_ylabel ( ylabel ) if lfooter is not None : ax . figure . text ( 0.005 , 0.005 , lfooter , ha = 'left' , va = 'bottom' , c = '#999999' , style = 'italic' , fontsize = 9 ) if rfooter is not None : ax . figure . text ( 0.995 , 0.005 , rfooter , ha = 'right' , va = 'bottom' , c = '#999999' , style = 'italic' , fontsize = 9 ) if concise_dates : ax . xaxis . set_major_formatter ( mdates . ConciseDateFormatter ( ax . xaxis . get_major_locator ( ) ) ) if straighten_tl : for tick in ax . get_xticklabels ( ) : tick . set_rotation ( 0 ) tick . set_ha ( 'center' ) ax . figure . tight_layout ( pad = 1.1 ) if title is not None : replaceable = list ( r'/\^:$' ) for char in replaceable : title = title . replace ( char , '' ) file_stem = f'{location}{title}' if 'save_suffix' in kwargs : file_stem += '-' + kwargs [ 'save_suffix' ] pathlib . Path ( location ) . mkdir ( parents = True , exist_ok = True ) ax . figure . savefig ( f'{file_stem}.png' , dpi = 300 ) if close : plt . show ( ) plt . close ( )
Complete and save a plot image
Complete and save a plot image
"""Complete and save a plot image""" # annotate the plot # format dates # straigten tick labels # close
ax
title
xlabel
ylabel
lfooter
rfooter
location
close
concise_dates
straighten_tl