When I am trying to execute the following code t2.jl
using Plots
f(x) = exp(-x^2/2)
plot(f, -3, 3)
by typing
julia t2.jl in the terminal, nothing is visualised.
But the same thing through Julia command line(or an IJulia notebook)
julia> using Plots
julia> f(x) = exp(-x^2/2)
f (generic function with 1 method)
julia> plot(f, -3, 3)
The plot visualises fine in both of the cases.
Can anyone help regarding this?
I am using julia 0.6.0
A plot is automatically displayed when it's returned. Otherwise, e.g. in your example in a file, you can use display (display(plot(f, -3, 3)) ) to show the plot.
@daschw, I am facing same exact issue as OP and your proposed solution did not help using Julia 1.1.0 on Ubuntu 18.04. Julia plots only display in a separate window at command line inside the julia shell but not when calling a .jl script. Nothing displays and no error is raised.
I attempted setting backend with gr() even gui() in script to no avail. Also, as you proposed I tried wrapping one and two display() (the latter of which outputs nothing in console). Still no plot appears. Should I open a new issue with reproducible example? Are there similar issues with confirmed solutions?
Oh, I think, I did not correctly understand the issue in the first place.
The display window is closed again because julia closes again after executing julia path/to/script.jl, I guess.
You can either start julia, and run the script from the REPL with include("path/to/script.jl"), or you add a saving command in the script, and inspect the image afterwards.
Thanks, @daschw, both suggestions work!
I have the same problem. And I think it is inefficient to save the figure and check it out. So I hope this problem can be solved recently.
And I think it is inefficient to save the figure and check it out.
I agree. That's why I suggest working interactively in the REPL or in Juno.
So I hope this problem can be solved recently
As I wrote above, if you execute julia path/to/script.jl, julia opens, runs the script and closes again. Hence, no julia window remains open. So this will not be fixed. Also, it's not really a Plots issue.
@daschw Yeah, you explained it clearly. And I did it successfully. Thanks for your sharing!
The command will keep the julia window remains open. And the figure will show.
julia -i path/to/script.jl
Also, I was wandering if it can be done with shell script without run julia in terminal. All the problem is about how to send a command to the REPL.
Just like that(in shell.sh file. of course, it is wrong answer.):
#! /bin/bash
include("path/to/script.jl") | julia
@daschw I try the following script. But it quit immediately, too. 馃槀
! /bin/bash
echo 'include("path/to/script.jl")' | julia
@daschw Also, I have a question about how to use Plots to draw figure in different canvas. For PyPlot, we can use figure(1), figure(2) to set. How to do that with Plots ?
To prevent julia from exiting immediately, add sleep(5) or readline() at the end of the script. readline() will wait for you to push enter.
@mkitti thanks for the answer, that's the expected behavior. Running julia -i ... drops into the REPL.
Most helpful comment
To prevent julia from exiting immediately, add
sleep(5)orreadline()at the end of the script.readline()will wait for you to push enter.