def finish_figure(fig, title=None, hspace=None, wspace=None):
    """Add finishing touches to a figure.

    This function can be used to add a title or adjust the spacing between
    subplot panels. The subplots will also be given a tight layout.

    Parameters
    ----------
    fig:
        The figure object to update.
    title: str
        The title of the figure. Underscores are automatically modified so
        LaTeX doesn't complain.
    hspace: float
        The amount of vertical space between subplots.
    wspace: float
        The amount of horizontal space between subplots.
    """

    if title:
        fig.suptitle(title.replace("_", r"\_"))

    fig.tight_layout(rect=[0.015, 0.015, 0.985, 0.985])

    if hspace is not None:
        fig.subplots_adjust(hspace=hspace)
    if wspace is not None:
        fig.subplots_adjust(wspace=wspace)

    return fig