Jupyter Notebook: Output image in previous line

前端 未结 2 481
灰色年华
灰色年华 2021-01-26 19:11

I want to plot some image side by side in my jupyter notebook. So it can save some space for display. For example

This is done through

fig = plt         


        
2条回答
  •  萌比男神i
    2021-01-26 20:12

    use the following align_figures():

    def align_figures():
        import matplotlib
        from matplotlib._pylab_helpers import Gcf
        from IPython.display import display_html
        import base64
        from ipykernel.pylab.backend_inline import show
    
        images = []
        for figure_manager in Gcf.get_all_fig_managers():
            fig = figure_manager.canvas.figure
            png = get_ipython().display_formatter.format(fig)[0]['image/png']
            src = base64.encodebytes(png).decode()
            images.append(''.format(src))
    
        html = "
    {}
    ".format("".join(images)) show._draw_called = False matplotlib.pyplot.close('all') display_html(html, raw=True)

    Here is a test:

    fig1, ax1 = pl.subplots(figsize=(4, 3))
    fig2, ax2 = pl.subplots(figsize=(4, 3))
    fig3, ax3 = pl.subplots(figsize=(4, 3))
    align_figures()
    

    The code assumes that the output format is PNG image.

提交回复
热议问题