【问题标题】:Plot two matplotlib.image.AxesImage objects side by side in Jupyter notebook在 Jupyter 笔记本中并排绘制两个 matplotlib.image.AxesImage 对象
【发布时间】:2017-05-29 04:40:46
【问题描述】:

我有一个返回 matplotlib.image.AxesImage 对象的函数,我想在同一个图中并排绘制其中两个对象。我尝试过这样的事情:

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

但是,这只会产生一个带有两个空子图的图形,而我想要的两个图被绘制在一列中,而不是并排绘制 (see current plots output here)。 我的问题是我必须使用function_that_returns_axes,我不知道如何将返回的图放入子图中。或者,如果我可以在 Jupyter 中并排显示两个图形,那也可以。

【问题讨论】:

  • 我认为我们需要更多关于function_that_returns_axes 的信息。似乎它确实考虑了代码的某些部分,否则它不会绘制到相同的现有图形。这个功能从何而来?你自己写的吗?能给个链接吗?
  • @ImportanceOfBeingErnest 当然,我正在使用 galpy 库 https://github.com/jobovy/galpy.git。特定的函数是 galpy/galpy/potential_src/Potential.py 中的绘图函数。感谢您的帮助!
  • 下面我的回答对你有用吗?如果是这样,我不需要深入研究 galpy 代码。
  • 我不知道为什么,但我收到了这个错误。 AttributeError:“AxesImage”对象没有属性“set_position”。我尝试了一些其他方法,看起来 set_cmap 和 set_colorbar 都可以工作,但是 set_title 和 set_position 给出了 AttributeError。
  • 好的,抱歉,我被function_that_returns_axes 误导了,因为它显然不返回轴而是图像(我知道你在代码的开头写了这个,但因为方法被调用不同, 我没注意到)。所以我用图像的方法更新了我的答案。

标签: matplotlib jupyter-notebook


【解决方案1】:

解决方案当然取决于function_that_returns_axes 对人物的确切作用。但看起来,它考虑了一个现有的图形并返回它绘制的轴。

然后可以使用此轴并更改其位置,使其位于用户定义的网格上。网格将通过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()

可以看出,两个轴彼此相邻,尽管它们最初是由function_that_returns_axes 创建的。

如果函数返回的不是坐标轴,而是图像,解决方法如下:

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...

【讨论】:

    【解决方案2】:

    如果您不能将ax1, ax2 传递给function_that_returns_axes,那么您可以使用以下代码:

    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)
    

    详情: Jupyter Notebook: Output image in previous line

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 2022-08-07
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      相关资源
      最近更新 更多