matplotlib and libpng issues with ipython notebook

11,407

Solution 1

I had this same problem on OS X Mavericks with libpng installed via homebrew and also XQuartz installed. It turned out matplotlib was finding the older XQuartz libpng version when compiling, but finding the more recent homebrew libpng at runtime.

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.

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

Solution 2

I had this same problem while trying to view images from OpenCV in the Jupyter Notebook while working in an Anaconda environment. Forcing a reinstall of matplotlib worked for me:

pip install -U --force-reinstall matplotlib

I found this method while looking at this GitHub link from Matt's solution.

Solution 3

For me putting

%matplotlib inline

before all matplotlib imports resolved this issue

Solution 4

I had this problem as well. Another solution is to change the format which the notebook will render images in, from 'png' to '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:

c.InlineBackend.figure_format = 'svg'

Solution 5

maybe its loading the wrong libpng at runtime. If you built matplotlib against libpng 1.5 make sure you also run with it. libpng 1.2 and 1.5 are not compatible.

in a shell you can set DYLD_LIBRARY_PATH to change the location the linker searches for libraries first.

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

should tell you what matplotlib is finding at runtime.

Share:
11,407
Admin
Author by

Admin

Updated on June 22, 2022

Comments

  • Admin
    Admin almost 2 years

    I was trying to use 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. When I tried to do either of them, an error message was returned "RuntimeError: Could not create write struct" resulting from execution of matplotlib. Also, a warning from the notebookApp prompt said "libpng warning: Application built with libpng-1.2.41 but running with 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).

    The configuration for my system is 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