# Buggy code
def _fig_to_img(fig, image_format="png", scale=None, **kwargs):
    """Plot figure and create a binary image."""
    # fig can be ndarray, mpl Figure, Mayavi Figure, or callable that produces
    # a mpl Figure
    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure

    if isinstance(fig, np.ndarray):
        fig = _ndarray_to_fig(fig)
    elif callable(fig):
        plt.close("all")
        fig = fig(**kwargs)
    elif not isinstance(fig, Figure):
        mlab = None
        try:
            mlab = _import_mlab()
        # on some systems importing Mayavi raises SystemExit (!)
        except Exception as e:
            warn(
                "Could not import mayavi (%r). Trying to render"
                "`mayavi.core.api.Scene` figure instances"
                " will throw an error." % (e,)
            )
        if fig.scene is not None:
            img = mlab.screenshot(figure=fig)
        else:  # Testing mode
            img = np.zeros((2, 2, 3))

        mlab.close(fig)
        fig = _ndarray_to_fig(img)

    output = BytesIO()
    if scale is not None:
        _scale_mpl_figure(fig, scale)
    logger.debug(
        "Saving figure %s with dpi %s" % (fig.get_size_inches(), fig.get_dpi())
    )
    with warnings.catch_warnings(record=True):
        warnings.simplefilter("ignore")  # incompatible axes
        fig.savefig(
            output, format=image_format, dpi=fig.get_dpi(), bbox_to_inches="tight"
        )
    plt.close(fig)
    output = output.getvalue()
    return (
        output.decode("utf-8")
        if image_format == "svg"
        else base64.b64encode(output).decode("ascii")
    )


# Traceback (AttributeError)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-a4d487ddeb86> in <module>()
3 rep = Report()
4 fig = plt.plot([1, 2], [1, 2])
----> 5 rep.add_figs_to_section(fig, 'my caption')

~/Desktop/projects/github_repos/mne-python/mne/report.py in add_figs_to_section(self, figs, captions, section, scale, image_format, comments, replace)
1078             img_klass = self._sectionvars[section]
1079
-> 1080             img = _fig_to_img(fig, image_format, scale)
1081             html = image_template.substitute(img=img, id=global_id,
1082                                              div_klass=div_klass,

~/Desktop/projects/github_repos/mne-python/mne/report.py in _fig_to_img(fig, image_format, scale, **kwargs)
74                  '`mayavi.core.api.Scene` figure instances'
75                  ' will throw an error.' % (e,))
---> 76         if fig.scene is not None:
77             img = mlab.screenshot(figure=fig)
78         else:  # Testing mode

AttributeError: 'Line2D' object has no attribute 'scene'

# Fixed code
def _fig_to_img(fig, image_format="png", scale=None, **kwargs):
    """Plot figure and create a binary image."""
    # fig can be ndarray, mpl Figure, Mayavi Figure, or callable that produces
    # a mpl Figure
    import matplotlib.pyplot as plt
    from matplotlib.figure import Figure

    if isinstance(fig, np.ndarray):
        fig = _ndarray_to_fig(fig)
    elif callable(fig):
        plt.close("all")
        fig = fig(**kwargs)
    elif not isinstance(fig, Figure):
        mlab = None
        try:
            mlab = _import_mlab()
        # on some systems importing Mayavi raises SystemExit (!)
        except Exception:
            is_mayavi = False
        else:
            import mayavi

            is_mayavi = isinstance(fig, mayavi.core.scene.Scene)
        if not is_mayavi:
            raise TypeError(
                "Each fig must be a matplotlib Figure, mayavi "
                "Scene, or NumPy ndarray, got %s (type %s)" % (fig, type(fig))
            )
        if fig.scene is not None:
            img = mlab.screenshot(figure=fig)
        else:  # Testing mode
            img = np.zeros((2, 2, 3))

        mlab.close(fig)
        fig = _ndarray_to_fig(img)

    output = BytesIO()
    if scale is not None:
        _scale_mpl_figure(fig, scale)
    logger.debug(
        "Saving figure %s with dpi %s" % (fig.get_size_inches(), fig.get_dpi())
    )
    with warnings.catch_warnings(record=True):
        warnings.simplefilter("ignore")  # incompatible axes
        fig.savefig(
            output, format=image_format, dpi=fig.get_dpi(), bbox_to_inches="tight"
        )
    plt.close(fig)
    output = output.getvalue()
    return (
        output.decode("utf-8")
        if image_format == "svg"
        else base64.b64encode(output).decode("ascii")
    )