Repository: ipython/ipython

Issue:
Cannot create empty figure in jupyter 4.0
I'm not sure if this is the right place or if I should address this to [matplotlib/matplotlib](https://github.com/matplotlib/matplotlib).

I was just wondering if the error was on my side or if something weird is going on:

```
Anaconda 2.3.0
Python 2.7.10
matplotlib 1.4.3
jupyter 1.0.0
ipython 4.0.0
```

Starting notebook via `jupyter notebook`

```
[In 1] %matplotlib inline
[In 2] import matplotlib
[In 3] fig = matplotlib.pyplot.figure()

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/Users/dlenz/anaconda/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
    335                 pass
    336             else:
--> 337                 return printer(obj)
    338             # Finally look for special method names
    339             method = _safe_get_formatter_method(obj, self.print_method)

/Users/dlenz/anaconda/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in <lambda>(fig)
    207         png_formatter.for_type(Figure, lambda fig: print_figure(fig, 'png', **kwargs))
    208     if 'retina' in formats or 'png2x' in formats:
--> 209         png_formatter.for_type(Figure, lambda fig: retina_figure(fig, **kwargs))
    210     if 'jpg' in formats or 'jpeg' in formats:
    211         jpg_formatter.for_type(Figure, lambda fig: print_figure(fig, 'jpg', **kwargs))

/Users/dlenz/anaconda/lib/python2.7/site-packages/IPython/core/pylabtools.pyc in retina_figure(fig,     **kwargs)
    124     """format a figure as a pixel-doubled (retina) PNG"""
    125     pngdata = print_figure(fig, fmt='retina', **kwargs)
--> 126     w, h = _pngxy(pngdata)
    127     metadata = dict(width=w//2, height=h//2)
    128     return pngdata, metadata

/Users/dlenz/anaconda/lib/python2.7/site-packages/IPython/core/display.pyc in _pngxy(data)
    607 def _pngxy(data):
    608     """read the (width, height) from a PNG header"""
--> 609     ihdr = data.index(b'IHDR')
    610     # next 8 bytes are width/height
    611     w4h4 = data[ihdr+4:ihdr+12]

AttributeError: 'NoneType' object has no attribute 'index'

<matplotlib.figure.Figure at 0x1077d4250>
```

If I do instead

```
[In 4] fig = matplotlib.pyplot.figure()
       matplotlib.pyplot.imshow(np.random.randn(100, 100))
```

it works like a charm.



Discussion:
Why are you trying to create a figure with inline?  The figure is destroyed as part of the display process.

Does this happen with `%matplotlib notebook`?

This is the right place, IPython maintains the inline backend.

Thanks for your quick response, the error was just due to my misunderstanding of how `inline` works.
It works fine with `%matplotlib notebook`.


Resolving patch:
diff --git a/IPython/core/pylabtools.py b/IPython/core/pylabtools.py
--- a/IPython/core/pylabtools.py
+++ b/IPython/core/pylabtools.py
@@ -123,6 +123,10 @@ def print_figure(fig, fmt='png', bbox_inches='tight', **kwargs):
 def retina_figure(fig, **kwargs):
     """format a figure as a pixel-doubled (retina) PNG"""
     pngdata = print_figure(fig, fmt='retina', **kwargs)
+    # Make sure that retina_figure acts just like print_figure and returns
+    # None when the figure is empty.
+    if pngdata is None:
+        return
     w, h = _pngxy(pngdata)
     metadata = dict(width=w//2, height=h//2)
     return pngdata, metadata


Test patch:
diff --git a/IPython/core/tests/test_pylabtools.py b/IPython/core/tests/test_pylabtools.py
--- a/IPython/core/tests/test_pylabtools.py
+++ b/IPython/core/tests/test_pylabtools.py
@@ -66,6 +66,11 @@ def test_figure_to_jpeg():
     assert jpeg.startswith(_JPEG)
 
 def test_retina_figure():
+    # simple empty-figure test
+    fig = plt.figure()
+    nt.assert_equal(pt.retina_figure(fig), None)
+    plt.close('all')
+
     fig = plt.figure()
     ax = fig.add_subplot(1,1,1)
     ax.plot([1,2,3])