[IPython-dev] graph displayed twice while overloading _repr_svg_ for notebook

Julien Schueller schueller at phimeca.com
Sun Jan 4 12:31:00 EST 2015




Hello,

I'm having trouble overloading _repr_svg_ for graph inlining in the notebook; my object is displayed twice if I call plt.plot just before.
I assumed a figure was left open or something, but even if I call plt.close('all') between the matplotlib calls, the result remains the same.

I did some imports first:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from scipy.stats import norm
import sys
import io
print('matplotlib version %s' % matplotlib.__version__)
import IPython
print('IPython version %s' % IPython.__version__)


Here'my object:
class NormGraph(object):
    def __init__(self):
        super(NormGraph, self).__init__()
    def _repr_svg_(self):
        if sys.version_info[0] >= 3:
            output = io.StringIO()
        else:
            output = io.BytesIO()
        self._fig = plt.figure()
        axes_kwargs = {}
        plot_kwargs = {}
        self._ax = [self._fig.add_subplot(111, **axes_kwargs)]
        x = np.arange(-10, 10, 0.001)
        y = norm.pdf(x,0,2)
        self._ax[0].plot(x, y, **plot_kwargs)
        self._fig.savefig(output, format='svg')
       
        return output.getvalue()


Then a basic call to pyplot:
%matplotlib inline
plt.plot(0, 1)

Then if I try to inline my object it's displayed twice:
NormGraph()

I atached the complete notebook.

-- 
Julien Schueller
Phimeca Engineering
www.phimeca.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Untitled0.ipynb
Type: application/octet-stream
Size: 40341 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150104/27afca35/attachment.obj>


More information about the IPython-dev mailing list