IPython + Pandas中的Matplotlib:导入错误 找不到符号:_png_create_info_struct

1 投票
1 回答
1701 浏览
提问于 2025-04-17 13:42

我在Python中安装了Matplotlib库,结果在导入它或者它的任何部分时都能正常工作,图表也能正常显示。

不过,我最近在看《Python数据分析》这本书,在第二章的IPython示例中,关于名字趋势的部分,当我运行以下代码时...

total_births.plot(title='Total births by sex and year')

我得到了...

ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib/_png.so, 2): Symbol not found: _png_create_info_struct

我在网上找到的大多数解决办法都是针对Python中Matplotlib的问题,但在IPython中却没什么用。

下面是整个IPython的输出内容:

ImportError                               Traceback (most recent call last)
<ipython-input-27-08d014b1a776> in <module>()
----> 1 total_births.plot(title='Total births by sex and year')

/Library/Python/2.7/site-packages/pandas/tools/plotting.pyc in plot_frame(frame, x, y,     subplots, sharex, sharey, use_index, figsize, grid, legend, rot, ax, style, title, xlim, ylim, logx, logy, xticks, yticks, kind, sort_columns, fontsize, secondary_y, **kwds)
   1453                      logy=logy, sort_columns=sort_columns,
   1454                      secondary_y=secondary_y, **kwds)
-> 1455     plot_obj.generate()
   1456     plot_obj.draw()
   1457     if subplots:

/Library/Python/2.7/site-packages/pandas/tools/plotting.pyc in generate(self)
    732         self._args_adjust()
    733         self._compute_plot_data()
--> 734         self._setup_subplots()
    735         self._make_plot()
    736         self._post_plot_logic()

/Library/Python/2.7/site-packages/pandas/tools/plotting.pyc in _setup_subplots(self)
    781         else:
    782             if self.ax is None:
--> 783                 fig = self.plt.figure(figsize=self.figsize)
    784                 ax = fig.add_subplot(111)
    785                 ax = self._maybe_right_yaxis(ax)

/Library/Python/2.7/site-packages/pandas/lib.so in pandas.lib.cache_readonly.__get__ (pandas/lib.c:27324)()

/Library/Python/2.7/site-packages/pandas/tools/plotting.pyc in plt(self)
    859     @cache_readonly
    860     def plt(self):
--> 861         import matplotlib.pyplot as plt
    862         return plt
    863 

/Library/Python/2.7/site-packages/matplotlib/pyplot.py in <module>()
     24 from matplotlib.cbook import dedent, silent_list, is_string_like, is_numlike
     25 from matplotlib import docstring
---> 26 from matplotlib.figure import Figure, figaspect
     27 from matplotlib.backend_bases import FigureCanvasBase
     28 from matplotlib.image import imread as _imread

/Library/Python/2.7/site-packages/matplotlib/figure.py in <module>()
     30 
     31 from matplotlib import _image
---> 32 from matplotlib.image import FigureImage
     33 
     34 import matplotlib.colorbar as cbar

    /Library/Python/2.7/site-packages/matplotlib/image.py in <module>()
         20 # For clarity, names from _image are given explicitly in this module:
         21 import matplotlib._image as _image
    ---> 22 import matplotlib._png as _png
         23 
         24 # For user convenience, the names from _image are also imported into

ImportError: dlopen(/Library/Python/2.7/site-packages/matplotlib/_png.so, 2): Symbol not found: _png_create_info_struct
  Referenced from: /Library/Python/2.7/site-packages/matplotlib/_png.so
  Expected in: flat namespace
 in /Library/Python/2.7/site-packages/matplotlib/_png.so

1 个回答

1

我在这个讨论串里找到了一个解决办法:ipython读取错误的python版本

@minrk提到的关于which pythonwhich ipython不匹配的问题,让我决定重写我电脑里/usr/local/bin/ipython文件的第一行。

撰写回答