在Jupyter笔记本中并排打印两个matplotlib.image.AxeImage对象

在Jupyter笔记本中并排打印两个matplotlib.image.AxeImage对象,matplotlib,jupyter-notebook,Matplotlib,Jupyter Notebook,我有一个返回matplotlib.image.AxeImage对象的函数,我想在同一个图中并排打印其中两个对象。我尝试了以下方法: fig, (ax1, ax2) = plt.subplots(ncols=2) ax1 = function_that_returns_AxesImage(some_arguments) # tried to reassign ax1 ax2 = function_that_returns_AxesImage(other_arguments) # tried to

我有一个返回matplotlib.image.AxeImage对象的函数,我想在同一个图中并排打印其中两个对象。我尝试了以下方法:

fig, (ax1, ax2) = plt.subplots(ncols=2)
ax1 = function_that_returns_AxesImage(some_arguments)  # tried to reassign ax1
ax2 = function_that_returns_AxesImage(other_arguments) # tried to reassign ax2
但是,这只会生成一个包含两个空子图的图形,我想要的两个图绘制在一列中,而不是并排()。
我的问题是我必须使用返回轴的函数,我不知道如何将返回的图放入子图中。或者,如果我可以在Jupyter中并排显示两个图形,也可以使用。

如果无法将
ax1、ax2
传递到
函数,该函数返回轴,则可以使用以下代码:

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('<img style="margin:0" align="left" src="data:image/png;base64,{}"/>'.format(src))

    html = "<div>{}</div>".format("".join(images))
    show._draw_called = False
    matplotlib.pyplot.close('all')
    display_html(html, raw=True)
def align_figures():
导入matplotlib
从matplotlib.\u pylab\u帮助程序导入Gcf
从IPython.display导入显示\u html
导入base64
从ipykernel.pylab.backend_内联导入显示
图像=[]
对于Gcf中的图形管理器。获取所有图形管理器():
图=图\u manager.canvas.figure
png=get_ipython().display_formatter.format(图)[0]['image/png']
src=base64.encodebytes(png).decode()
images.append(“”.format(src))
html=“{}”格式(“.join(图像))
show.\u draw\u called=False
matplotlib.pyplot.close('all')
显示html(html,原始=真)
详情如下:
解决方案当然取决于
函数返回的轴对图形的具体作用。但看起来,它考虑了现有图形,并返回了已绘制的轴

然后,可以取这个轴并更改其位置,使其位于用户定义的网格上。网格将通过
matplotlib.gridspec
创建

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure()

def function_that_returns_axes(l="A"):
    ax = fig.add_subplot(111, label=l)
    ax.plot(np.random.rand(5))
    return ax

ax1 = function_that_returns_axes("A")
ax2 = function_that_returns_axes("B")

gs = gridspec.GridSpec(1,2)
ax1.set_position(gs[0].get_position(fig))
ax1.set_subplotspec(gs[0]) 

ax2.set_position(gs[1].get_position(fig))
ax2.set_subplotspec(gs[1])   

plt.show()

可以看出,两个轴彼此相邻,尽管它们最初是由返回轴的
函数创建的

如果函数不返回轴,而是返回图像,则解决方案如下:

def function_that_returns_image(l="A"):
    ax = fig.add_subplot(111, label=l)
    im = ax.imshow(np.random.rand(5,5))
    return im

im1 = function_that_returns_image("A")  
im2 = function_that_returns_image("B")

ax1 = im1.axes
ax2 = im2.axes

# the rest being the same as above...

我想我们需要更多关于返回轴的
函数的信息。看起来它确实考虑了代码的某些部分,否则它不会绘制到相同的现有图形。这个函数来自哪里?是你自己写的吗?你能给我一个链接吗?@ImportanceOfBeingErnest当然,我在高比图书馆工作。特定函数是galpy/galpy/potential\u src/potential.py中的plot函数。谢谢你的帮助!我下面的回答对你有用吗?如果是这样的话,我就不需要深入研究galpy代码了。我不知道为什么,但我得到了这个错误。AttributeError:“AxeImage”对象没有属性“set\u position”。我尝试过其他一些方法,它看起来像set_cmap和set_colorbar都能工作,但是set_title和set_position给出了AttributeError。好的,对不起,我被返回轴的
函数误导了,因为它显然不是返回轴而是返回图像(我知道你在代码的开头写的,但是因为方法调用不同,我没有注意到)。所以我用图像的方法更新了我的答案。