1
votes

I have recently begun learning to use Julia, converting over from Matlab/Octave. I decided that the best way to get some experience was to convert some code I was already working on i Octave - a Newton solver for a complicated multidimensional problem. I have been able to convert the code over successfully (and with noticeable speedup relative to Octave, without devectorisation or other performance-based changes), with only one issue arising.

I have chosen to use PyPlot for plotting, due to its similarity to Matlab/Octave's plotting functionality. However, there is some behaviour from PyPlot that is undesired. I use the plotting function to display the current state of the vector I am trying to get to zero (using the Newton solver part of the code), so that I can see what it is doing, and adjust the code to try to improve this behaviour. I input the number of Newton steps to take before the code stops, and then I can make adjustments or re-issue the command to continue attempting to converge.

I have the code set up to plot the current state every few steps, so that I can, for instance, have the code take 200 steps, but show me the status after every 10 steps. In Octave, this works perfectly, providing me with up-to-date information - should the behaviour of the code not be desirable, I can quickly cancel the code with Ctrl-C (this part works in Julia, too).

However, Julia does not produce or update the plots when the plot() command is used; instead, it produces the plot, or updates it if the plot window is already open, only when the code finishes. This entirely defeats the purpose of the intermittent plotting within the code. Once the code has completed, the plot is correctly generated, so I know that the plot() command itself is being used correctly.

I have tried adding either draw() or show() immediately after the plot command. I have also tried display(gcf()). None of these have modified the result. I have confirmed that isinteractive() outputs "true". I have also tried turning interactivity off (ioff()) and switching whether to use the python or julia backend (pygui(true) and pygui(false)), with no effect on this behaviour.

Have I missed something? Is there another package or option that needs to be set in order to force PyPlot to generate the current plot immediately, rather than waiting until Julia finishes its current code run to generate the plot?

Or is it perhaps possible that scope is causing a problem, here, as the intermittent plotting happens inside a while loop?

I am using xubuntu 12.10 with Julia 0.2.1.

1
@Isaiah - Thank you - perhaps you could post as an answer (with direct mention of "yield"), so that I can mark it as a correct answer, give you rep points, and make things easier for anyone else looking for a resolution to this problem. I did a number of searches for information on how to address this, and that PyPlot issue page never showed up for me. - Glen O
There has recently been a discussion of something that's probably related on the Google group. groups.google.com/forum/#!topic/julia-users/Wb5hgyj2PMY - Jeremy

1 Answers

0
votes

PyPlot defaults to this behavior in the REPL. To make it show the plots as they are plotted type ion(). To turn it off again type ioff().

ion() is only effective for the current season so if you want it to stay on across sessions just add it to your .juliarc file.

If you're using iPython ion() will plot to a new window but ioff() will plot inline.