Prophet: ImportError: cannot import name '_png' from 'matplotlib'

Created on 26 Sep 2020  Â·  4Comments  Â·  Source: facebook/prophet

Hi, while trying out the fbprophet tutorial, I encountered the following error log when attempting to execute in jupyter notebook the following code: fig1 = m.plot(forecast)

The error log is as follows:


ImportError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
339 pass
340 else:
--> 341 return printer(obj)
342 # Finally look for special method names
343 method = get_real_method(obj, self.print_method)

~/opt/anaconda3/lib/python3.7/site-packages/IPython/core/pylabtools.py in (fig)
246
247 if 'png' in formats:
--> 248 png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', *kwargs))
249 if 'retina' in formats or 'png2x' in formats:
250 png_formatter.for_type(Figure, lambda fig: retina_figure(fig, *
kwargs))

~/opt/anaconda3/lib/python3.7/site-packages/IPython/core/pylabtools.py in print_figure(fig, fmt, bbox_inches, *kwargs)
130 FigureCanvasBase(fig)
131
--> 132 fig.canvas.print_figure(bytes_io, *
kw)
133 data = bytes_io.getvalue()
134 if fmt == 'svg':

~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
2092 hardcopy.
2093
-> 2094 Parameters
2095 ----------
2096 filename : str or path-like or file-like

~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/backend_bases.py in _get_renderer(figure, print_method)
1558 return renderer
1559 else:
-> 1560 raise RuntimeError(f"{print_method} did not call Figure.draw, so "
1561 f"no renderer is available")
1562 finally:

~/opt/anaconda3/lib/python3.7/site-packages/matplotlib/backends/backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, args, *kwargs)
503 pil_kwargs : dict, optional
504 Keyword arguments passed to PIL.Image.Image.save.
--> 505
506 If the 'pnginfo' key is present, it completely overrides
507 metadata, including the default 'Software' key.

ImportError: cannot import name '_png' from 'matplotlib' (/Users/jasonchia/opt/anaconda3/lib/python3.7/site-packages/matplotlib/__init__.py)

I tried updating matplotlib to 3.3.2 but the problem still persists. Please advise, am in need of urgent assistance...Thanks in advance.

Most helpful comment

try this pip install matplotlib==3.1.3 work for me

All 4 comments

I am experiencing the same issue when trying to plot a simple line plot:

[<matplotlib.lines.Line2D at 0x21c8e387748>]
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~\Anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
    339                 pass
    340             else:
--> 341                 return printer(obj)
    342             # Finally look for special method names
    343             method = get_real_method(obj, self.print_method)

~\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in <lambda>(fig)
    246 
    247     if 'png' in formats:
--> 248         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    249     if 'retina' in formats or 'png2x' in formats:
    250         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))

~\Anaconda3\lib\site-packages\IPython\core\pylabtools.py in print_figure(fig, fmt, bbox_inches, **kwargs)
    130         FigureCanvasBase(fig)
    131 
--> 132     fig.canvas.print_figure(bytes_io, **kw)
    133     data = bytes_io.getvalue()
    134     if fmt == 'svg':

~\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, bbox_inches, **kwargs)
   2092         hardcopy.
   2093 
-> 2094         Parameters
   2095         ----------
   2096         filename : str or path-like or file-like

~\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in _get_renderer(figure, print_method)
   1558             return renderer
   1559         else:
-> 1560             raise RuntimeError(f"{print_method} did not call Figure.draw, so "
   1561                                f"no renderer is available")
   1562         finally:

~\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, metadata, pil_kwargs, *args, **kwargs)
    503         pil_kwargs : dict, optional
    504             Keyword arguments passed to `PIL.Image.Image.save`.
--> 505 
    506             If the 'pnginfo' key is present, it completely overrides
    507             *metadata*, including the default 'Software' key.

ImportError: cannot import name '_png' from 'matplotlib' (C:\Users\Semseman\Anaconda3\lib\site-packages\matplotlib\__init__.py)

<Figure size 432x288 with 1 Axes>

@jasonchia89 it looks like something is messed up in the matplotlib install. I haven't seen this before and I'm not exactly sure what is going on, but poking around on stackoverflow a bit, it looks like it might be an issue with libpng. Otherwise, it looks like downgrading to matplotlib 3.1 might be worth a shot.

try this pip install matplotlib==3.1.3 work for me

try this pip install matplotlib==3.1.3 work for me

It worked for me too, thank you!!

Was this page helpful?
0 / 5 - 0 ratings