import io
import pickle


def copy_fig(fig):
        """Copy the figure to avoid reference passing

        Returns
        -------
        Figure
            the new figure object
        """
        # Creating the buffer in which to pickle the fig
        buf = io.BytesIO()
        pickle.dump(fig, buf)
        buf.seek(0)
        # Loading the figure in a new figure
        fig_dock = pickle.load(buf)
        for i, ax in enumerate(fig_dock.axes):
            axes = ax
        return fig_dock