我想用IPython笔记本电脑来交互式分析我用Biopython的基因组图模块制作的一些基因组图。虽然有大量关于如何使用matplotlib在IPython笔记本中获得内联图形的文档,但GenomeDiagram使用ReportLab工具包,我认为IPython不支持该工具包。

然而,我在想,一种解决这个问题的方法是将绘图/基因组图写入一个文件,然后内联打开图像,结果会是这样的:

gd_diagram.write("test.png", "PNG")
display(file="test.png")

然而,我不知道如何做到这一点,也不知道这是否可行。那么有人知道IPython中是否可以打开/显示图像吗?


当前回答

通过这篇文章,你可以做到以下几点:

from IPython.display import Image
Image(filename='test.png') 

(官方文档)

其他回答

如果你想有效地显示大量的图像,我建议使用IPyPlot包

import ipyplot

ipyplot.plot_images(images_array, max_images=20, img_width=150)

该包中还有一些其他有用的功能,您可以在交互式选项卡(每个标签/类的单独选项卡)中显示图像,这对所有ML分类任务非常有帮助。

这将在Jupyter中导入并显示。jpg图像(在Anaconda环境中使用Python 2.7测试)

from IPython.display import display
from PIL import Image


path="/path/to/image.jpg"
display(Image.open(path))

您可能需要安装PIL

在《Anaconda》中,这是通过输入完成的

conda install pillow

通过这篇文章,你可以做到以下几点:

from IPython.display import Image
Image(filename='test.png') 

(官方文档)

另一个选择是:

from matplotlib import pyplot as plt 
from io import BytesIO
from PIL import Image
import Ipython

f = BytesIO()
plt.savefig(f, format='png')
Ipython.display.display(Ipython.display.Image(data=f.getvalue()))
f.close()

你可以直接使用这个,不用进口PIL

from IPython.display import Image, display
    
    display(Image(base_image_path))