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

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

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

Julien Schueller-2


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()
            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:

I atached the complete notebook.

Julien Schueller
Phimeca Engineering

IPython-User mailing list
[hidden email]

Untitled0.ipynb (53K) Download Attachment