[IPython-dev] graph displayed twice while overloading _repr_svg_ for notebook
Antonino Ingargiola
tritemio at gmail.com
Mon Jan 5 14:06:50 EST 2015
Hi,
I don't know if it helps but with plain matplotlib you can close the figure
and call display(fig) on the closed figure. I use this trick to plot
several figures in a loop (closing them at each iteration) or for updating
plots via widgets.
Antonio
On Mon, Jan 5, 2015 at 9:43 AM, Thomas Kluyver <takowl at gmail.com> wrote:
> If you've done '%matplotlib inline' previously, when you create a figure
> inside a cell, it will automatically be displayed at the end of that cell,
> separately from the regular repr system. So your _repr_svg_ method is
> getting called once, and the figure you create in that is being displayed
> again. I'm not sure what the best way to deal with that is.
>
> Thomas
>
> On 4 January 2015 at 17:31, Julien Schueller <schueller at phimeca.com>
> wrote:
>
>>
>>
>>
>> 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
>>
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>>
>>
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150105/1d446bfa/attachment.html>
More information about the IPython-dev
mailing list