Plotting fails silently?

I have a fresh install of Julia(on Catalina), with Plots added via Pkg. Then I try to plot a simple line:

using Plots; println("imported")
x = 1:10; y = rand(10); # These are the plotting data
println("tryna plot"); plot(x, y); println("you see?")

Running this file via julia try.jl just prints out the two prints. (Although, it happily plots to GSKTerm from REPL).
My question is, why does it fail silently? Shouldn’t Julia tell me that it didn’t find any backends to complete plot(x,y)? Is it a design choice(in which case, where can I read more about it)?

Why not end with a non-zero exit code at least?

Thank You
Warm Regards

Edit: Of course above code will never display the plot, as ; suppresses it. But my initial attempts were without the ;, which was added for brevity. I shan’t fix the code above as it will take away from @Volker’s instructive comment.

It is not failing, the script exits and you do not have time to see the plot. Add a savefig("plot.png") and you will get the result in that file. Or else you need to pause the script, for example adding a read statement after plotting.

1 Like

Thank you, saving the plot confirms that it is indeed plotting properly.

But, if I pause the script via sleep(60), I still don’t see the output anywhere, in GSKTerm or any of the browsers.

I cannot test that now, but perhaps adding display() will force the plot to show up.

1 Like

Hi,

You can simply write it without ;, if you write each command in a single line.

using Plots
println("imported")
x = 1:10 
y = rand(10) # These are the plotting data
println("tryna plot") 
plot(x, y) 
println("you see?")

I use ; at the end of line just, if I would like to plot more things on the axis or to create a subplot and don´t want to see the interim plots, i.e.:

p1 = plot(x, y);
plot!(p1, x, 10*y)

or

p1 = plot(x, y);
p2 = plot(x, 10*y);
plot(p1,p2, layout=(2,1))

if you would like to write in a single line then you need the semicolon to separate the commands. In this case use display() or gui().

2 Likes

Indeed, it does show up in GKSTerm. So, what happens to the plot otherwise? It simply gets computed but not displayed huh?
Shouldn’t it get displayed due to plot(x,y) anyway, as that line returns the plot, and, as mentioned here

A Plot is only displayed when returned (a semicolon will suppress the return)

Thank You so much for the clarifications.

I appreciate your help, but I was using the semi-colons to cut down on the number of lines while also being able to ‘see’ whats going on. For example, the first println told me that using Plots takes an awful lot of time to happen(which still has me bewildered).

Also, in your code, what does ! do?

Docs here told me that

function names that end with an exclamation mark modify one or more of their arguments by convention

But I still don’t grok plot!, which parameter is changed by !? and why?

My apologies for the barrage of questions.
Thank You

The plot itself. You can add curves or change the plot properties using that.

plot()
plot!(rand(3),rand(3),label="first")
plot!(rand(3),rand(3),label="second")
plot!(xlabel="x",ylabel="y")
savefig("plot.pdf")
2 Likes

Whoa! that’s amazing,
This composability is so reminiscent of Mathematica.
P.S. python doesn’t have something like this does it?

Take a look at the examples of Plots. And I have this text with some ideas: Tips to create beautiful, publication-quality plots in Julia - Nextjournal

Lately I’ve been exploring plotting only, thank you again for resources.