4

I am trying to plot a function evolving in real time with Julia.

For that, I saw that GR package could be used in Julia, when I try to apply exactly the example given here :

import GR
GR.inline("mov")
x = [0:0.01:2*pi]

    for i = 1:200
    GR.plot(x, sin.(x + i / 10.0))
    end

GR.show()

I get the following error message while executing the loop part :

expected Real or Complex

in #plot_args#12(::Symbol, ::Function, ::Tuple{Array{FloatRange{Float64},1},Array{Array{Float64,1},1}}) at /Users/myname/.julia/v0.5/GR/src/jlgr.jl:936 ....

I have looked a bit on internet and found this where someone seems to have a similar problem but I really dont understand the answers and what should I do to make it work.

I can also just find an other way to plot in real time (within a loop).

Can someone help with that please?

Thank you by advance

ChrlTsr
  • 149
  • 1
  • 7

1 Answers1

5

Here is a solution that uses Plots to plot to GR. I am sure this can be done in GR directly as well, but not sure what is wrong with your example.

using Plots
gr(show = true) # in IJulia this would be: gr(show = :ijulia)
x = 0:0.01:2*pi
for i in 1:200 
    display(plot(x, sin.(x + i / 10.0)))
end

Note that this example is real-time (as per the question) and thus may lag a little. In the code in the example, a gif is created instead, which is then displayed to ijulia.

Michael K. Borregaard
  • 7,864
  • 1
  • 28
  • 35
  • Judging from the screenshot provided, I get the impression he's trying to do it specifically in IJulia rather than the generic REPL. – Tasos Papastylianou May 31 '17 at 15:49
  • This works. I think 'gr(show = :ijulia)' was necessary! Thank you – ChrlTsr Jun 01 '17 at 08:58
  • Sorry... The first time it worked but now sometimes it works and sometimes I get directly all the curves at the end of the loop, it does not work in real time all the times, with exactly the same code. Do you have an idea of what happens? – ChrlTsr Jun 01 '17 at 09:14
  • I think it's got something to do with how the Jupyter notebook is updated, it's not the best context for real-time plotting IMHO. Perhaps try to post in the discourse thread I linked above ( discourse.julialang.org/t/juno-and-interactive-plot/3989/2), Josef Heinen who responded there is the creator of GR and most likely the author of the original notebook. – Michael K. Borregaard Jun 01 '17 at 09:26
  • I have added 'display' above, which makes the answer more general but not strictly necessary in ijulia. – Michael K. Borregaard Jun 01 '17 at 09:30
  • Thank you. I posted my question on the link provided, I hope to get it solved. Adding "display" as you suggest gives me this at each loop : "1-element Array{Any,1}: PyObject " , but does not change the plot. Is that normal? – ChrlTsr Jun 02 '17 at 10:28
  • Is there a way to do this in VS code without generating hundreds of new figures? Like, just have one figure open and get overwritten each iteration? – M. Thompson Jul 28 '22 at 23:20