On Sat, Oct 6, 2012 at 6:51 PM, Fernando Perez <span dir="ltr"><<a href="mailto:fperez.net@gmail.com" target="_blank">fperez.net@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Skipper,<br>
<div class="im"><br>
On Sat, Oct 6, 2012 at 7:32 AM, Skipper Seabold <<a href="mailto:jsseabold@gmail.com">jsseabold@gmail.com</a>> wrote:<br>
> Hi,<br>
><br>
> It's a matplotlib question, but the conclusion on how to work with mpl is<br>
> due to IPython (and programs that interact with mpl similarly) AFAICT.<br>
><br>
> So I'm curious if someone can go into a bit more detail about the<br>
> conclusions of this thread [1]. Namely,<br>
><br>
> "don't call show or draw_if_interactive in your library code"<br>
><br>
> Or rather, what are the exact pitfalls here. I don't understand the possible<br>
> side effects. Is it still true that draw_if_interactive doesn't play well<br>
> with the notebooks?<br>
<br>
</div>No, that shouldn't be a problem. The issue is that show() will try to<br>
pop up a window, which could potentially trigger GUI activity. show()<br>
is a function whose entire purpose is to cause a side-effect: the<br>
creation of the figure itself.<br>
<br>
The standard practice is therefore to put a single show() call at the<br>
end of a script so that figure windows only open at the end. I do<br>
sometimes put the show() calls earlier, but I do it knowingly because<br>
I *want* figures to pop up half-way through executions.<br>
<div> </div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
> We have plotting functions that are designed to update a given axes. I often<br>
> work in interactive mode, and I'd like it if these functions updated my axes<br>
> in the way that I expect (and an R user doing plotting in Python would<br>
> expect). But now I'm forced to litter my user scripts with<br>
> draw_if_interactive rather than handle this for the users in the plotting<br>
> functions. Do I have any other options?<br>
<br>
</div>Is a single<br>
<br>
plt.show()<br>
<br>
as the last line of your scripts not viable? That's sort of the<br>
standard advice we always have given for years. By delaying the draw<br>
calls, you also gain potentially a lot in performance: matplotlib can<br>
render the whole figure just once, rather than doing it over and over<br>
at potentially quadratic costs.<br>
<br>
Fundamentally the message is: show()/draw() are designed to produce a<br>
side effect, the display of a rendered figure. Since this side effect<br>
is only meaningful to a human looking at the screen, the decision to<br>
invoke them should be in the hands of said human, not taken by<br>
intermediate libraries that have no way of knowing the context in<br>
which they are being called.<br>
<br>
Does that make sense?<br></blockquote><div><br>Sure. I'm not trying to take this ability away from the user. draw_if_interactive is innocuous if the user is not using interactive = True. IMO having set interactive to True, though, the user essentially says, when I plot something or update an existing plot, then do it. Don't wait. Maybe this is what I'm not understanding. Consider this example<br>
<br>def plot_line(ax, x, y):<br> reg = sm.OLS(y, sm.add_constant(x, prepend=False)).fit()<br> grid = np.linspace(x.min(), x.max(), 50)<br> y_hat = reg.predict(sm.add_constant(grid, prepend=False))<br> ax.plot(grid, y_hat)<br>
return ax.get_figure()<br><br>import matplotlib.pyplot as plt<br>plt.interactive(True)<br>x = np.random.random(size=25)<br>y = 2.5 * x + 2 + np.random.normal(size=25)<br>fig, ax = plt.subplots()<br>ax.scatter(x, y)<br>
fig = plot_line(ax, x, y)<br><br>Without a call in plot_line to draw_if_interactive, the existing plot is not updated with the line. But if our plotting functions call draw_if_interactive, then the user gets what is expected (since interactive is True).<br>
<br>I'm not proposing using draw/show, but the advice of the previous thread is not to use draw_if_interactive, which, I believe, is a different issue right?<br><br>Skipper<br><br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Cheers,<br>
<br>
f<br>
_______________________________________________<br>
IPython-User mailing list<br>
<a href="mailto:IPython-User@scipy.org">IPython-User@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-user" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-user</a><br>
</blockquote></div><br>