【问题标题】:Python matplotlib and libpng Incompatibility issuePython matplotlib 和 libpng 不兼容问题
【发布时间】:2013-03-19 19:41:06
【问题描述】:

我真的被这个问题困扰了这么久。
原来,在用 matplotlib 绘图后,我可以很容易地保存图像。
但是,安装 scipy 后,我无法再保存我的图像了。
(我使用 pip 安装了 matplot 和 scipy。)
我试图查找一些信息,但仍然无法解决问题。
我的操作系统是 Mac OS X Lion (10.7)

我认为以下链接是一些相关问题

https://github.com/ipython/ipython/issues/2710
Matplotlib pylab savefig runtime error in python 3.2.3
matplotlib and libpng issues with ipython notebook
libpng15 static link issues

看来如果我可以重新链接库或设置 DYLD_LIBRARY_PATH(实际上我不知道那是什么......)

或者也许我必须重新编译一些东西?
顺便说一句,我对基于 linux 的系统非常陌生,所以如果有人能以相对简单的方式解释它,那就太好了。 非常感谢。

以下是一些错误信息:

libpng warning: Application was compiled with png.h from libpng-1.5.4
libpng warning: Application  is  running with png.c from libpng-1.4.10
libpng warning: Incompatible libpng version in application and library
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/Library/Python/2.7/site-packages/matplotlib/backends/backend_macosx.pyc in save_figure(self, *args)
    476         if filename is None: # Cancel
    477             return
--> 478         self.canvas.print_figure(filename)
    479 
    480     def prepare_configure_subplots(self):

/Library/Python/2.7/site-packages/matplotlib/backend_bases.pyc in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2094                 orientation=orientation,
   2095                 bbox_inches_restore=_bbox_inches_restore,
-> 2096                 **kwargs)
   2097         finally:
   2098             if bbox_inches and restore_bbox:

/Library/Python/2.7/site-packages/matplotlib/backend_bases.pyc in print_png(self, *args, **kwargs)
   1856         from backends.backend_agg import FigureCanvasAgg # lazy import
   1857         agg = self.switch_backends(FigureCanvasAgg)
-> 1858         return agg.print_png(*args, **kwargs)
   1859 
   1860     def print_ps(self, *args, **kwargs):

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

RuntimeError: Could not create write struct

【问题讨论】:

  • 你想解决这个 PNG 问题还是只想在你的 Mac 上使用 matplotlib?
  • 是的,我必须解决 PNG 问题,因为某些原因我必须保存图像。
  • 保存 PNG?例如,一种解决方法是解决 JPG。
  • 是的,我已经考虑过了,但似乎我仍然必须先将其保存为 PNG。 (见:stackoverflow.com/questions/8827016/…
  • 不正确。 plt.savefig('output.jpg') 有效,除非您特别没有安装 JPG 支持。

标签: python matplotlib libpng


【解决方案1】:

如果您保存 JPG,则不需要 PNG 支持。也不需要 PIL:

import pylab as pl
pl.plot([0.2,0.3,0.4], [0.1,0.2,0.3], label='series name')
pl.xlabel('x label')
pl.ylabel('y label')
pl.ylim([0.0, 1.0])
pl.xlim([0.0, 1.0])
pl.title('Title')
pl.legend(loc="lower left")
pl.savefig('output.jpg')
pl.show()

【讨论】:

  • 更好的选择,IMO:另存为 pdf!它是矢量图形,你会得到一些有趣的东西,比如可选择文本。
猜你喜欢
  • 2018-07-29
  • 2023-01-08
  • 2012-11-28
  • 2013-04-04
  • 1970-01-01
  • 1970-01-01
  • 2018-06-02
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多