简体   繁体   English

ipython 笔记本的 matplotlib 和 libpng 问题

[英]matplotlib and libpng issues with ipython notebook

I was trying to use ipython notebook .我试图使用 ipython notebook 。 I installed all the dependency libraries.我安装了所有依赖库。 However, I cannot use either the "--pylab=inline" option when launching ipython or "savefig" function in the Ipython console.但是,在启动 ipython 或 Ipython 控制台中的“savefig”函数时,我不能使用“--pylab=inline”选项。 When I tried to do either of them, an error message was returned "RuntimeError: Could not create write struct" resulting from execution of matplotlib.当我尝试执行其中任何一个时,由于执行 matplotlib,返回了一条错误消息“RuntimeError:无法创建写入结构”。 Also, a warning from the notebookApp prompt said "libpng warning: Application built with libpng-1.2.41 but running with 1.5.13".此外,来自 notebookApp 提示的警告说“libpng 警告:使用 libpng-1.2.41 构建但使用 1.5.13 运行的应用程序”。

However, I installed the newest libpng(1.5.13), uninstalled matplotlib with pip uninstall and reinstalled matplotlib with pip install (and during the build process, I can see that libpng1.5.13 is used for the building of matplotlib).但是,我安装了最新的 libpng(1.5.13),使用 pip uninstall 卸载了 matplotlib,并使用 pip install 重新安装了 matplotlib(在构建过程中,我可以看到 libpng1.5.13 用于构建 matplotlib)。

The configuration for my system is Mac OS X10.6, python2.7.我系统的配置是Mac OS X10.6,python2.7。 Anybody has similar experience or some suggestiongs?有没有人有类似的经历或者建议?

Here are the traceback errors:以下是回溯错误:

[<matplotlib.lines.Line2D at 0x106066d50>]
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/zmq/pylab/backend_inline.pyc in show(close)
    100     try:
    101         for figure_manager in Gcf.get_all_fig_managers():
--> 102             send_figure(figure_manager.canvas.figure)
    103     finally:
    104         show._to_draw = []

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/zmq/pylab/backend_inline.pyc in send_figure(fig)
    209     """
    210     fmt = InlineBackend.instance().figure_format
--> 211     data = print_figure(fig, fmt)
    212     # print_figure will return None if there's nothing to draw:
    213     if data is None:

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in print_figure(fig, fmt)
    102     try:
    103         bytes_io = BytesIO()
--> 104         fig.canvas.print_figure(bytes_io, format=fmt, bbox_inches='tight')
    105         data = bytes_io.getvalue()
    106     finally:

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2050                     orientation=orientation,
   2051                     dryrun=True,
-> 2052                     **kwargs)
   2053                 renderer = self.figure._cachedRenderer
   2054                 bbox_inches = self.figure.get_tightbbox(renderer)

/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/backends/backend_agg.pyc in print_png(self, filename_or_obj, *args, **kwargs)
    501             _png.write_png(renderer._renderer.buffer_rgba(),
    502                            renderer.width, renderer.height,
--> 503                            filename_or_obj, self.figure.dpi)
    504         finally:
    505             if close:

RuntimeError: Could not create write struct

Thanks a lot,非常感谢,

Jie

I had this same problem on OS X Mavericks with libpng installed via homebrew and also XQuartz installed.我在 OS X Mavericks 上遇到了同样的问题,通过自制软件安装了 libpng 并安装了 XQuartz。 It turned out matplotlib was finding the older XQuartz libpng version when compiling, but finding the more recent homebrew libpng at runtime.结果证明 matplotlib 在编译时找到了较旧的 XQuartz libpng 版本,但在运行时找到了更新的自制 libpng。

The best solution I've found is from this comment by jaengelberg on github : Uninstall matplotlib, temporarily rename the XQuartz libpng headers so they can't be found, install matplotlib, then change the header names back.我找到的最佳解决方案来自jaengelberg 在 github 上的评论:卸载 matplotlib,暂时重命名 XQuartz libpng 标头,以便找不到它们,安装 matplotlib,然后改回标头名称。

Here it is in full:这是完整的:

pip uninstall matplotlib
cd /opt/X11/include/libpng15
sudo mv png.h _png.h
sudo mv pngconf.h _pngconf.h
sudo mv pnglibconf.h _pnglibconf.h
pip install matplotlib
sudo mv _png.h png.h
sudo mv _pngconf.h pngconf.h
sudo mv _pnglibconf.h pnglibconf.h

I had this same problem while trying to view images from OpenCV in the Jupyter Notebook while working in an Anaconda environment.在 Anaconda 环境中工作时,我尝试在 Jupyter Notebook 中查看来自 OpenCV 的图像时遇到了同样的问题。 Forcing a reinstall of matplotlib worked for me:强制重新安装 matplotlib 对我有用:

pip install -U --force-reinstall matplotlib

I found this method while looking at this GitHub link from Matt's solution.我从 Matt 的解决方案中查看这个 GitHub 链接时发现了这种方法。

For me putting对我来说

%matplotlib inline

before all matplotlib imports resolved this issue在所有 matplotlib 导入解决此问题之前

I had this problem as well.我也有这个问题。 Another solution is to change the format which the notebook will render images in, from 'png' to 'svg'.另一种解决方案是将笔记本渲染图像的格式从“png”更改为“svg”。 This can be done in your config file .这可以在您的配置文件中完成。 Mine is located at:我的位于:

~/.ipython/profile_default/ipython_notebook_config.py

There is a line that looks like this有一条线看起来像这样

# c.InlineBackend.figure_format = 'png'

Uncommenting and changing to 'svg' did the trick for me:取消注释并更改为“svg”对我有用:

c.InlineBackend.figure_format = 'svg'

maybe its loading the wrong libpng at runtime.也许它在运行时加载了错误的 libpng。 If you built matplotlib against libpng 1.5 make sure you also run with it.如果您针对 libpng 1.5 构建 matplotlib,请确保您也使用它运行。 libpng 1.2 and 1.5 are not compatible. libpng 1.2 和 1.5 不兼容。

in a shell you can set DYLD_LIBRARY_PATH to change the location the linker searches for libraries first.在 shell 中,您可以设置 DYLD_LIBRARY_PATH 以更改链接器首先搜索库的位置。

otool -L /<somepath>/matplotlib/_png.so

should tell you what matplotlib is finding at runtime.应该告诉你 matplotlib 在运行时发现了什么。

I had almost the exact same experience:我有几乎完全相同的经历:

  • Exactly the same "RuntimeError: Could not create write struct" in IPython Notebook IPython Notebook 中完全相同的“RuntimeError:无法创建写入结构”
  • Exactly the same "libpng warning: Application built with libpng-1.2.41 but running with 1.5.13"完全相同的“libpng 警告:使用 libpng-1.2.41 构建但使用 1.5.13 运行的应用程序”
  • Same OS: Mac OS X10.6相同的操作系统:Mac OS X10.6
  • py33-ipython @0.13.1_0+notebook+parallel (from MacPorts) py33-ipython @0.13.1_0+notebook+parallel(来自 MacPorts)
  • python3.3 (from MacPorts) in place of your python2.7 (only diff I see from you) python3.3(来自MacPorts)代替您的python2.7(我从您那里看到的唯一差异)

I think the MacPorts recipe for IPython may have some problems for some OS versions.我认为 IPython 的 MacPorts 配方可能对某些操作系统版本有一些问题。

Not sure where you got your python2.7 from, but my solution was that I ultimately ditched MacPorts in favor of homebrew (which also had some problems, but I was able to resolve them by installing some packages directly from sources, see this question ).不知道你从哪里得到你的python2.7,但我的解决方案是我最终放弃了MacPorts,转而使用自制软件(这也有一些问题,但我能够通过直接从源代码安装一些包来解决它们,见这个问题) .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM