from io import StringIO


def svg(plot, close=True):
    """
    Creates SVG code and closes plot.
    
    Args:
        plot: matplotlib.pyplot
            Plot from which the SVG should be made.
    
    Returns:
        str
            SVG code.
    """
    
    # make SVG
    svg_file = StringIO()
    plot.savefig(svg_file, format='svg')
    svg = svg_file.getvalue()
    svg_file.close()
    
    # close plot
    if close:
        plot.close()
    
    return svg