# Buggy code
def display_figure(fig, message=None, max_width="100%"):
    "Display widgets applicable to the specified element"
    if OutputMagic.options["fig"] == "repr":
        return None

    figure_format = OutputMagic.options["fig"]
    dpi = OutputMagic.options["dpi"]
    css = OutputMagic.options["css"]
    backend = OutputMagic.options["backend"]

    if backend == "nbagg" and new_figure_manager_given_figure is not None:
        manager = new_figure_manager_given_figure(OutputMagic.nbagg_counter, fig)
        # Need to call mouse_init on each 3D axis to enable rotation support
        for ax in fig.get_axes():
            if isinstance(ax, Axes3D):
                ax.mouse_init()
        OutputMagic.nbagg_counter += 1
        manager.show()
        return ""
    elif backend == "d3" and mpld3:
        fig.dpi = dpi
        mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fontsize=14))
        html = "<center>" + mpld3.fig_to_html(fig) + "<center/>"
    else:
        renderer = Store.renderer.instance(dpi=dpi)
        figdata = renderer.figure_data(fig, figure_format)
        if figure_format == "svg":
            figdata = figdata.encode("utf-8")
        b64 = base64.b64encode(figdata).decode("utf-8")
        (mime_type, tag) = MIME_TYPES[figure_format], HTML_TAGS[figure_format]
        src = HTML_TAGS["base64"].format(mime_type=mime_type, b64=b64)
        html = tag.format(src=src, css=dict_to_css(css))
    plt.close(fig)
    return html if (message is None) else "<b>%s</b></br>%s" % (message, html)


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/holoviews/ipython/display_hooks.py", line 203, in wrapped
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/holoviews/ipython/display_hooks.py", line 233, in element_display
**opts(element, get_plot_size(element, size)))()
File "/usr/local/lib/python2.7/dist-packages/holoviews/plotting/raster.py", line 34, in __init__
super(RasterPlot, self).__init__(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/holoviews/plotting/element.py", line 132, in __init__
uniform=uniform, **dict(params, **plot_opts))
File "/usr/local/lib/python2.7/dist-packages/holoviews/plotting/plot.py", line 114, in __init__
self.handles['axis'] = self._init_axis(axis)
File "/usr/local/lib/python2.7/dist-packages/holoviews/plotting/plot.py", line 248, in _init_axis
fig = plt.figure()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 435, in figure
**kwargs)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4agg.py", line 47, in new_figure_manager
return new_figure_manager_given_figure(num, thisFig)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4agg.py", line 54, in new_figure_manager_given_figure
canvas = FigureCanvasQTAgg(figure)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4agg.py", line 72, in __init__
FigureCanvasQT.__init__(self, figure)
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt4.py", line 68, in __init__
_create_qApp()
File "/usr/local/lib/python2.7/dist-packages/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp
raise RuntimeError('Invalid DISPLAY variable')
RuntimeError: Invalid DISPLAY variable

# Fixed code
def display_figure(fig, message=None, allow_nbagg=True, max_width="100%"):
    "Display widgets applicable to the specified element"
    if OutputMagic.options["fig"] == "repr":
        return None

    figure_format = OutputMagic.options["fig"]
    dpi = OutputMagic.options["dpi"]
    css = OutputMagic.options["css"]
    backend = OutputMagic.options["backend"]

    if (
        allow_nbagg
        and backend == "nbagg"
        and new_figure_manager_given_figure is not None
    ):
        manager = new_figure_manager_given_figure(OutputMagic.nbagg_counter, fig)
        # Need to call mouse_init on each 3D axis to enable rotation support
        for ax in fig.get_axes():
            if isinstance(ax, Axes3D):
                ax.mouse_init()
        OutputMagic.nbagg_counter += 1
        manager.show()
        return ""
    elif backend == "d3" and mpld3:
        fig.dpi = dpi
        mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fontsize=14))
        html = "<center>" + mpld3.fig_to_html(fig) + "<center/>"
    else:
        renderer = Store.renderer.instance(dpi=dpi)
        figdata = renderer.figure_data(fig, figure_format)
        if figure_format == "svg":
            figdata = figdata.encode("utf-8")
        b64 = base64.b64encode(figdata).decode("utf-8")
        (mime_type, tag) = MIME_TYPES[figure_format], HTML_TAGS[figure_format]
        src = HTML_TAGS["base64"].format(mime_type=mime_type, b64=b64)
        html = tag.format(src=src, css=dict_to_css(css))
    plt.close(fig)
    return html if (message is None) else "<b>%s</b></br>%s" % (message, html)