<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def figure(key=None, fig=None, **kwargs):
 """Creates figures and switches between figures. If a ``bqplot.Figure`` object is provided via the fig optional argument, this figure becomes the current context figure. Otherwise: - If no key is provided, a new empty context figure is created. - If a key is provided for which a context already exists, the corresponding context becomes current. - If a key is provided and no corresponding context exists, a new context is reated for that key and becomes current. Besides, optional arguments allow to set or modify Attributes of the selected context figure. Parameters key: hashable, optional Any variable that can be used as a key for a dictionary fig: Figure, optional A bqplot Figure """

    scales_arg = kwargs.pop('scales', {})
    _context['current_key'] = key
    if fig is not None:                                     # fig provided
        _context['figure'] = fig
        if key is not None:
            _context['figure_registry'][key] = fig
        for arg in kwargs:
            setattr(_context['figure'], arg, kwargs[arg])
    else:                                                   # no fig provided
        if key is None:                                     # no key provided
            _context['figure'] = Figure(**kwargs)
        else:                                               # a key is provided
            if key not in _context['figure_registry']:
                if 'title' not in kwargs:
                    kwargs['title'] = 'Figure' + ' ' + str(key)
                _context['figure_registry'][key] = Figure(**kwargs)
            _context['figure'] = _context['figure_registry'][key]
            for arg in kwargs:
                setattr(_context['figure'], arg, kwargs[arg])
    scales(key, scales=scales_arg)
    # Set the axis reference dictionary. This dictionary contains the mapping
    # from the possible dimensions in the figure to the list of scales with
    # respect to which axes have been drawn for this figure.
    # Used to automatically generate axis.
    if(getattr(_context['figure'], 'axis_registry', None) is None):
        setattr(_context['figure'], 'axis_registry', {})
    return _context['figure']