Plots in VSCode

Hello

I am using the Julia extension in VSCode.
I am a little confused with how plotting works in it.

I have something like this in a Julia script

Grid_Size = LinRange(1E0, 1E5, 100)
Log_of_Size = map(log, Grid_Size)
plot(Grid_Size, Log_of_Size)

When I do Julia: Execute File REPL no plot pane appears. I have to enclose the plot command in display() to get the plot pane to appear.

Is this the intended behavior? It seemed more natural to just have to include the line plot(Grid_Size, Grid_Size, label="Linear Scaling") for a plot to show up. I’m probably missing something. I’d appreciate any thoughts.

p = plot(x,y,...) # create the plot object
display(p) # display the plot object

tells julia to display the object p. IMO this is better than having it implicitly show up on some display without user control.

1 Like

Thanks - I’ll do that. I’m just confused why just doing plot() doesn’t seem to be working in this particular script file. If I create a new file and do

using Plots

plot(1:1:10, map(x -> x^2, 1:1:10))

It appears to work fine (a plot appears in the plot pane).

The last expression in the file will be displayed automatically.

Doh! Thanks - is that a feature of julia scripts or the VS Code extension?

Both, but I’d say it’s best not to rely on it.

Hm - I just created a file and put the following

a = 5
b = 6.0
c = a * b

I ran it from the terminal and it didn’t output anything. Based on what you said above, it should have returned 30.0 right?

This is true if you include a file within a Julia session (e.g. in the REPL, IJulia…), but not if you run it from the shell. There you have to explicitly show the result, either with @show, println… or display in the case of plots, as already explained.

1 Like