# Buggy code
def _plot_mpl_stc(
    stc,
    subject=None,
    surface="inflated",
    hemi="lh",
    colormap="auto",
    time_label="auto",
    smoothing_steps=10,
    subjects_dir=None,
    views="lat",
    clim="auto",
    figure=None,
    initial_time=None,
    time_unit="s",
    background="black",
    spacing="oct6",
    time_viewer=False,
    colorbar=True,
    transparent=True,
):
    """Plot source estimate using mpl."""
    import matplotlib.pyplot as plt
    from matplotlib import cm
    from matplotlib.widgets import Slider
    import nibabel as nib
    from scipy import stats
    from ..morph import _get_subject_sphere_tris

    if hemi not in ["lh", "rh"]:
        raise ValueError(
            "hemi must be 'lh' or 'rh' when using matplotlib. Got %s." % hemi
        )
    lh_kwargs = {
        "lat": {"elev": 0, "azim": 180},
        "med": {"elev": 0, "azim": 0},
        "ros": {"elev": 0, "azim": 90},
        "cau": {"elev": 0, "azim": -90},
        "dor": {"elev": 90, "azim": -90},
        "ven": {"elev": -90, "azim": -90},
        "fro": {"elev": 0, "azim": 106.739},
        "par": {"elev": 30, "azim": -120},
    }
    rh_kwargs = {
        "lat": {"elev": 0, "azim": 0},
        "med": {"elev": 0, "azim": 180},
        "ros": {"elev": 0, "azim": 90},
        "cau": {"elev": 0, "azim": -90},
        "dor": {"elev": 90, "azim": -90},
        "ven": {"elev": -90, "azim": -90},
        "fro": {"elev": 16.739, "azim": 60},
        "par": {"elev": 30, "azim": -60},
    }
    time_viewer = False if time_viewer == "auto" else time_viewer
    kwargs = dict(lh=lh_kwargs, rh=rh_kwargs)
    views = "lat" if views == "auto" else views
    _check_option("views", views, sorted(lh_kwargs.keys()))
    mapdata = _process_clim(clim, colormap, transparent, stc.data)
    _separate_map(mapdata)
    colormap, scale_pts = _linearize_map(mapdata)
    del transparent, mapdata

    time_label, times = _handle_time(time_label, time_unit, stc.times)
    fig = plt.figure(figsize=(6, 6)) if figure is None else figure
    ax = fig.gca(projection="3d")
    hemi_idx = 0 if hemi == "lh" else 1
    surf = op.join(subjects_dir, subject, "surf", "%s.%s" % (hemi, surface))
    if spacing == "all":
        coords, faces = nib.freesurfer.read_geometry(surf)
        inuse = slice(None)
    else:
        stype, sval, ico_surf, src_type_str = _check_spacing(spacing)
        surf = _create_surf_spacing(surf, hemi, subject, stype, ico_surf, subjects_dir)
        inuse = surf["vertno"]
        faces = surf["use_tris"]
        coords = surf["rr"][inuse]
        shape = faces.shape
        faces = stats.rankdata(faces, "dense").reshape(shape) - 1
        faces = np.round(faces).astype(int)  # should really be int-like anyway
    del surf
    vertices = stc.vertices[hemi_idx]
    n_verts = len(vertices)
    tris = _get_subject_sphere_tris(subject, subjects_dir)[hemi_idx]
    cmap = cm.get_cmap(colormap)
    greymap = cm.get_cmap("Greys")

    curv = nib.freesurfer.read_morph_data(
        op.join(subjects_dir, subject, "surf", "%s.curv" % hemi)
    )[inuse]
    curv = np.clip(np.array(curv > 0, np.int64), 0.33, 0.66)
    params = dict(
        ax=ax,
        stc=stc,
        coords=coords,
        faces=faces,
        hemi_idx=hemi_idx,
        vertices=vertices,
        tris=tris,
        smoothing_steps=smoothing_steps,
        n_verts=n_verts,
        inuse=inuse,
        cmap=cmap,
        curv=curv,
        scale_pts=scale_pts,
        greymap=greymap,
        time_label=time_label,
        time_unit=time_unit,
    )
    _smooth_plot(initial_time, params)

    ax.view_init(**kwargs[hemi][views])

    try:
        ax.set_facecolor(background)
    except AttributeError:
        ax.set_axis_bgcolor(background)

    if time_viewer:
        time_viewer = figure_nobar(figsize=(4.5, 0.25))
        fig.time_viewer = time_viewer
        ax_time = plt.axes()
        if initial_time is None:
            initial_time = 0
        slider = Slider(
            ax=ax_time,
            label="Time",
            valmin=times[0],
            valmax=times[-1],
            valinit=initial_time,
        )
        time_viewer.slider = slider
        callback_slider = partial(_smooth_plot, params=params)
        slider.on_changed(callback_slider)
        callback_key = partial(_key_pressed_slider, params=params)
        time_viewer.canvas.mpl_connect("key_press_event", callback_key)

        time_viewer.subplots_adjust(left=0.12, bottom=0.05, right=0.75, top=0.95)
    fig.subplots_adjust(left=0.0, bottom=0.0, right=1.0, top=1.0)

    # add colorbar
    from mpl_toolkits.axes_grid1.inset_locator import inset_axes

    sm = plt.cm.ScalarMappable(
        cmap=cmap, norm=plt.Normalize(scale_pts[0], scale_pts[2])
    )
    cax = inset_axes(ax, width="80%", height="5%", loc=8, borderpad=3.0)
    plt.setp(plt.getp(cax, "xticklabels"), color="w")
    sm.set_array(np.linspace(scale_pts[0], scale_pts[2], 256))
    if colorbar:
        cb = plt.colorbar(sm, cax=cax, orientation="horizontal")
        cb_yticks = plt.getp(cax, "yticklabels")
        plt.setp(cb_yticks, color="w")
        cax.tick_params(labelsize=16)
        cb.patch.set_facecolor("0.5")
        cax.set(xlim=(scale_pts[0], scale_pts[2]))
    plt.show()
    return fig


# Traceback (KeyError)
Using control points [6.56993144e-11 7.80732925e-11 1.82440323e-10]
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/miniconda3/envs/hnn_dev/lib/python3.7/site-packages/matplotlib/projections/__init__.py in get_projection_class(projection)
57     try:
---> 58         return projection_registry.get_projection_class(projection)
59     except KeyError:

~/miniconda3/envs/hnn_dev/lib/python3.7/site-packages/matplotlib/projections/__init__.py in get_projection_class(self, name)
24         """
---> 25         return self._all_projection_types[name]
26

KeyError: '3d'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~/src/git/hnn-core/examples/plot_simulate_somato.py in <module>
----> 1 stc_mne.plot(backend='matplotlib')

~/src/git/mne-python/mne/source_estimate.py in plot(self, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, brain_kwargs, verbose)
662             src=src, volume_options=volume_options, view_layout=view_layout,
663             add_data_kwargs=add_data_kwargs, brain_kwargs=brain_kwargs,
--> 664             verbose=verbose)
665         return brain
666

<decorator-gen-142> in plot_source_estimates(stc, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, brain_kwargs, verbose)

~/src/git/mne-python/mne/viz/_3d.py in plot_source_estimates(stc, subject, surface, hemi, colormap, time_label, smoothing_steps, transparent, alpha, time_viewer, subjects_dir, figure, views, colorbar, clim, cortex, size, background, foreground, initial_time, time_unit, backend, spacing, title, show_traces, src, volume_options, view_layout, add_data_kwargs, brain_kwargs, verbose)
1826         transparent=transparent)
1827     if plot_mpl:
-> 1828         return _plot_mpl_stc(stc, spacing=spacing, **kwargs)
1829     return _plot_stc(
1830         stc, overlay_alpha=alpha, brain_alpha=alpha, vector_alpha=alpha,

~/src/git/mne-python/mne/viz/_3d.py in _plot_mpl_stc(***failed resolving arguments***)
1524     time_label, times = _handle_time(time_label, time_unit, stc.times)
1525     fig = plt.figure(figsize=(6, 6)) if figure is None else figure
-> 1526     ax = fig.gca(projection='3d')
1527     hemi_idx = 0 if hemi == 'lh' else 1
1528     surf = op.join(subjects_dir, subject, 'surf', '%s.%s' % (hemi, surface))

~/miniconda3/envs/hnn_dev/lib/python3.7/site-packages/matplotlib/figure.py in gca(self, **kwargs)
1930
1931         # no axes found, so create one which spans the figure
-> 1932         return self.add_subplot(1, 1, 1, **kwargs)
1933
1934     def sca(self, a):

~/miniconda3/envs/hnn_dev/lib/python3.7/site-packages/matplotlib/figure.py in add_subplot(self, *args, **kwargs)
1394         else:
1395             projection_class, kwargs, key = \
-> 1396                 self._process_projection_requirements(*args, **kwargs)
1397
1398             # try to find the axes with this key in the stack

~/miniconda3/envs/hnn_dev/lib/python3.7/site-packages/matplotlib/figure.py in _process_projection_requirements(self, polar, projection, *args, **kwargs)
1118
1119         if isinstance(projection, str) or projection is None:
-> 1120             projection_class = projections.get_projection_class(projection)
1121         elif hasattr(projection, '_as_mpl_axes'):
1122             projection_class, extra_kwargs = projection._as_mpl_axes()

~/miniconda3/envs/hnn_dev/lib/python3.7/site-packages/matplotlib/projections/__init__.py in get_projection_class(projection)
58         return projection_registry.get_projection_class(projection)
59     except KeyError:
---> 60         raise ValueError("Unknown projection %r" % projection)
61
62

ValueError: Unknown projection '3d'

# Fixed code
def _plot_mpl_stc(
    stc,
    subject=None,
    surface="inflated",
    hemi="lh",
    colormap="auto",
    time_label="auto",
    smoothing_steps=10,
    subjects_dir=None,
    views="lat",
    clim="auto",
    figure=None,
    initial_time=None,
    time_unit="s",
    background="black",
    spacing="oct6",
    time_viewer=False,
    colorbar=True,
    transparent=True,
):
    """Plot source estimate using mpl."""
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D  # noqa: F401, analysis:ignore
    from matplotlib import cm
    from matplotlib.widgets import Slider
    import nibabel as nib
    from scipy import stats
    from ..morph import _get_subject_sphere_tris

    if hemi not in ["lh", "rh"]:
        raise ValueError(
            "hemi must be 'lh' or 'rh' when using matplotlib. Got %s." % hemi
        )
    lh_kwargs = {
        "lat": {"elev": 0, "azim": 180},
        "med": {"elev": 0, "azim": 0},
        "ros": {"elev": 0, "azim": 90},
        "cau": {"elev": 0, "azim": -90},
        "dor": {"elev": 90, "azim": -90},
        "ven": {"elev": -90, "azim": -90},
        "fro": {"elev": 0, "azim": 106.739},
        "par": {"elev": 30, "azim": -120},
    }
    rh_kwargs = {
        "lat": {"elev": 0, "azim": 0},
        "med": {"elev": 0, "azim": 180},
        "ros": {"elev": 0, "azim": 90},
        "cau": {"elev": 0, "azim": -90},
        "dor": {"elev": 90, "azim": -90},
        "ven": {"elev": -90, "azim": -90},
        "fro": {"elev": 16.739, "azim": 60},
        "par": {"elev": 30, "azim": -60},
    }
    time_viewer = False if time_viewer == "auto" else time_viewer
    kwargs = dict(lh=lh_kwargs, rh=rh_kwargs)
    views = "lat" if views == "auto" else views
    _check_option("views", views, sorted(lh_kwargs.keys()))
    mapdata = _process_clim(clim, colormap, transparent, stc.data)
    _separate_map(mapdata)
    colormap, scale_pts = _linearize_map(mapdata)
    del transparent, mapdata

    time_label, times = _handle_time(time_label, time_unit, stc.times)
    fig = plt.figure(figsize=(6, 6)) if figure is None else figure
    ax = fig.gca(projection="3d")
    hemi_idx = 0 if hemi == "lh" else 1
    surf = op.join(subjects_dir, subject, "surf", "%s.%s" % (hemi, surface))
    if spacing == "all":
        coords, faces = nib.freesurfer.read_geometry(surf)
        inuse = slice(None)
    else:
        stype, sval, ico_surf, src_type_str = _check_spacing(spacing)
        surf = _create_surf_spacing(surf, hemi, subject, stype, ico_surf, subjects_dir)
        inuse = surf["vertno"]
        faces = surf["use_tris"]
        coords = surf["rr"][inuse]
        shape = faces.shape
        faces = stats.rankdata(faces, "dense").reshape(shape) - 1
        faces = np.round(faces).astype(int)  # should really be int-like anyway
    del surf
    vertices = stc.vertices[hemi_idx]
    n_verts = len(vertices)
    tris = _get_subject_sphere_tris(subject, subjects_dir)[hemi_idx]
    cmap = cm.get_cmap(colormap)
    greymap = cm.get_cmap("Greys")

    curv = nib.freesurfer.read_morph_data(
        op.join(subjects_dir, subject, "surf", "%s.curv" % hemi)
    )[inuse]
    curv = np.clip(np.array(curv > 0, np.int64), 0.33, 0.66)
    params = dict(
        ax=ax,
        stc=stc,
        coords=coords,
        faces=faces,
        hemi_idx=hemi_idx,
        vertices=vertices,
        tris=tris,
        smoothing_steps=smoothing_steps,
        n_verts=n_verts,
        inuse=inuse,
        cmap=cmap,
        curv=curv,
        scale_pts=scale_pts,
        greymap=greymap,
        time_label=time_label,
        time_unit=time_unit,
    )
    _smooth_plot(initial_time, params)

    ax.view_init(**kwargs[hemi][views])

    try:
        ax.set_facecolor(background)
    except AttributeError:
        ax.set_axis_bgcolor(background)

    if time_viewer:
        time_viewer = figure_nobar(figsize=(4.5, 0.25))
        fig.time_viewer = time_viewer
        ax_time = plt.axes()
        if initial_time is None:
            initial_time = 0
        slider = Slider(
            ax=ax_time,
            label="Time",
            valmin=times[0],
            valmax=times[-1],
            valinit=initial_time,
        )
        time_viewer.slider = slider
        callback_slider = partial(_smooth_plot, params=params)
        slider.on_changed(callback_slider)
        callback_key = partial(_key_pressed_slider, params=params)
        time_viewer.canvas.mpl_connect("key_press_event", callback_key)

        time_viewer.subplots_adjust(left=0.12, bottom=0.05, right=0.75, top=0.95)
    fig.subplots_adjust(left=0.0, bottom=0.0, right=1.0, top=1.0)

    # add colorbar
    from mpl_toolkits.axes_grid1.inset_locator import inset_axes

    sm = plt.cm.ScalarMappable(
        cmap=cmap, norm=plt.Normalize(scale_pts[0], scale_pts[2])
    )
    cax = inset_axes(ax, width="80%", height="5%", loc=8, borderpad=3.0)
    plt.setp(plt.getp(cax, "xticklabels"), color="w")
    sm.set_array(np.linspace(scale_pts[0], scale_pts[2], 256))
    if colorbar:
        cb = plt.colorbar(sm, cax=cax, orientation="horizontal")
        cb_yticks = plt.getp(cax, "yticklabels")
        plt.setp(cb_yticks, color="w")
        cax.tick_params(labelsize=16)
        cb.patch.set_facecolor("0.5")
        cax.set(xlim=(scale_pts[0], scale_pts[2]))
    plt.show()
    return fig