From: Jean-Michel P. <jea...@ir...> - 2004-09-30 08:14:29
|
jdh...@ac... wrote: > In truth, the latest release of matplotlib sets a flag on show to > prevent repeated calls from doing any real damage. > > But for classroom use, I suggest you just put "interactive : True" in > your rc file and then you have no need for show. Is there a downside > to this approach? > > JDH It seems that show() hangs if no figure has been created before calling (under matplotlib 0.62.4). Am I wrong or is it an unexpected use of show() ? JM. Philippe |
From: Jean-Michel P. <jea...@ir...> - 2004-10-04 06:44:16
|
Ok. Now suppose you write an application that runs a set of algorithms not known in advance. These algorithms may or may not create figures depending on what they perform; they may also encounter difficulties (e.g. not enough input data) so that none of them is finally able to create a figure. As this is always better to dissociate code pieces the more as possible, I'd prefer not to use a global variable to trace figure creation. So is there a way to know that no figure was created? Regards. JM. jdh...@ac... wrote: > Jean-Michel> It seems that show() hangs if no figure has been > Jean-Michel> created before calling (under matplotlib 0.62.4). Am > Jean-Michel> I wrong or is it an unexpected use of show() ? > > show should be the last line of your script. It is expected to hang. > It starts the GUI mainloop after which all processing is done in the > GUI event handling (unless you are using threading). > > See http://matplotlib.sf.net/faq.html#SHOW > > JDH |
From: John H. <jdh...@ac...> - 2004-10-08 14:38:38
|
>>>>> "Jean-Michel" == Jean-Michel Philippe <jea...@ir...> writes: Jean-Michel> Ok. Now suppose you write an application that runs a Jean-Michel> set of algorithms not known in advance. These Jean-Michel> algorithms may or may not create figures depending on Jean-Michel> what they perform; they may also encounter Jean-Michel> difficulties (e.g. not enough input data) so that Jean-Michel> none of them is finally able to create a figure. As Jean-Michel> this is always better to dissociate code pieces the Jean-Michel> more as possible, I'd prefer not to use a global Jean-Michel> variable to trace figure creation. So is there a way Jean-Michel> to know that no figure was created? In 0.63, we introduced a flag on the matplotlib.backends.draw_if_interactive function. If draw_if_interactive._called is False, the function was never called and thus no plotting commands were issued. You may be able to use this to decide whether to call show or not. Eg , at the end of your script if draw_if_interactive._called: show() Note if you are using a pure image backend (eg agg, svg, ps) you do not need show at all; this is for GUI backends only. Just call savefig anywhere in your code. But if you really want full control with no magic globals, I suggest using the matplotlib API rather than the matlab interface. Here is a minimal example with the SVG backend to create a figure w/o the matlab interface from matplotlib.backends.backend_svg import FigureCanvasSVG from matplotlib.figure import Figure fig = Figure() ax = fig.add_subplot(111) ax.plot([1,2,3]) canvas = FigureCanvasSVG(fig) canvas.print_figure('myfile.svg') There are several examples called embedding_in_*.py at http://matplotlib.sf.net/examples that show how to do this for your GUI of choice. Let me know if you need more help; and be sure to tell which backend you are targetting. JDH |
From: Jean-Michel P. <jea...@ir...> - 2004-10-11 08:17:37
|
jdh...@ac... wrote: > In 0.63, we introduced a flag on the > matplotlib.backends.draw_if_interactive function. If I must say I'm really impressed how fast matplotlib gets improved! > Eg , at the end of your script > > if draw_if_interactive._called: show() > > Note if you are using a pure image backend (eg agg, svg, ps) you do > not need show at all; this is for GUI backends only. Just call > savefig anywhere in your code. Perfect! This is exactly what I needed for one of my apps. > But if you really want full control with no magic globals, I suggest > using the matplotlib API rather than the matlab interface. Here is a > minimal example with the SVG backend to create a figure w/o the matlab > interface Well, what do you exactly mean by full control? The fact that the figure is no more controlled by matplotlib.matlab (as matlab does) but under my own control? So that the application is now fully responsible for displaying it? I'm afraid I don't understand why this should remove "magic" globals, I feel some globals are still required... NB: currently I'm targeting TkAgg. JM. Philippe |
From: John H. <jdh...@ac...> - 2004-10-11 13:33:58
|
>>>>> "Jean-Michel" == Jean-Michel Philippe <jea...@ir...> writes: Jean-Michel> Perfect! This is exactly what I needed for one of my Jean-Michel> apps. Glad that worked for you. >> But if you really want full control with no magic globals, I >> suggest using the matplotlib API rather than the matlab >> interface. Here is a minimal example with the SVG backend to >> create a figure w/o the matlab interface Jean-Michel> Well, what do you exactly mean by full control? The Jean-Michel> fact that the figure is no more controlled by Jean-Michel> matplotlib.matlab (as matlab does) but under my own Jean-Michel> control? So that the application is now fully Jean-Michel> responsible for displaying it? Jean-Michel> NB: currently I'm targeting TkAgg. What I mean is that is that if you want to explicitly control when your figure windows are shown, you need to use the matplotlib API, and example of which for tkagg is at http://matplotlib.sf.net/examples/embedding_in_tk.py. In this case, you explicitly make the calls to show or hide your window when you want. If you use the matlab interface, you are constrained either to 1) if interactive is False, show all of your figures at the end of the script when you call show or 2) if interactive is True, show all your windows at the time of their creation. Actually, you may have a 3rd (untested, unsupported) option with the matlab interface. Thanks to the changes Fernando and I introduced to support ipython, I believe as of matplotlib 0.62 it is safe to call show repeatedly without blocking script execution. You may want to test this and report back. Jean-Michel> I'm afraid I don't understand why this should remove Jean-Michel> "magic" globals, I feel some globals are still Jean-Michel> required... Some people do not like to use the matlab interface because it manages the current figure and axes for them, behind the scenes. Eg when you type plot(x,y), the plot command is sent to the current figure and axes, as in matlab, which are stored as "global" (actually module level) variables in matplotlib.matlab. In the matplotlib API, you have to explicitly instantiate the figure and axes, and direct your plotting, saving, etc commands to these instances, as in from matplotlib.numerix import arange, sin, pi from matplotlib.figure import Figure f = Figure(figsize=(5,4), dpi=100) a = f.add_subplot(111) t = arange(0.0,3.0,0.01) s = sin(2*pi*t) a.plot(t,s) In a complex, nested application or script, it is sometimes nice to have this extra degree of clarity. Hope this helps, JDH |
From: John H. <jdh...@ac...> - 2004-09-30 14:34:00
|
>>>>> "Jean-Michel" == Jean-Michel Philippe <jea...@ir...> writes: Jean-Michel> It seems that show() hangs if no figure has been Jean-Michel> created before calling (under matplotlib 0.62.4). Am Jean-Michel> I wrong or is it an unexpected use of show() ? show should be the last line of your script. It is expected to hang. It starts the GUI mainloop after which all processing is done in the GUI event handling (unless you are using threading). See http://matplotlib.sf.net/faq.html#SHOW JDH |