How to display the Plots by executing the file from command line

I’m trying to execute the below code that display a plot:

using Plots, Measures
pyplot()

data = [rand(100), rand(100)];

histogram(data, layout = 2,
          title = ["Dataset A" "Dataset B"], legend = false,
          ylabel = "ylabel", margin = 5mm)

I do not get any output once I execute it from the command line!!

enter image description here

Knowing that I tried it in 3 different ways and it works (Jupyter, Juni, Julia session), though I’m confused about the way it works in the Juno, kinldy see my observations below:

With Jupyter it is functioning perfectly:

With Juno, I’ve to run the file TWICE!! the first time looks to be for compilation, second time for execution, maybe!!
And if I closed the plot, I’ve to close the Julia session, restart it, then re-execute the file TWICE!! and sometimes nothing is appearing!!

With Julia session, it take time for first time execution, then if I closed the plot and run it again, it is appearing smoothly.

enter image description here

enter image description here

Thanks for the friends in Stackoverflow community, I got an answer here, reposting it for the Julia community:

The commands like histogram or plot typically doesn’t display the plots for users, they only generate and return the plots.
What displays the plots is actually the display system in Julia.
When Julia is in interactive use, like in REPL, Jupyter and Juno, the display system will be invoked automatically with commands not ending with “;”. That’s why you see plots displayed in REPL, Jupyter and Juno.
But when executing the file from the command line, the display system is not automatically activated. So you first have to invoke display yourself like this:

using Plots, Measures
pyplot()

data = [rand(100), rand(100)];

h = histogram(data, layout = 2,
              title = ["Dataset A" "Dataset B"], legend = false,
              ylabel = "ylabel", margin = 5mm)
display(h)

But even this will not give you the picture, but only text representation of the plot. This is because in command line julia, only a very simple text display system is in place, and it doesn’t have “full” support for plots from Plots. To display the plots, you have to write your own display mechanism and push it to Julia display system, which is not hard but a little tedious. I will give an example when I have more time.

BTW, if you just want plots generated from command line, another way is to save it to files, which is more direct than making a display mechanism yourself.

Update
Here is a simple display, which mimics the display system used in Julia REPL. The code here is for Julia 0.7/1.0.

const output = IOBuffer()
using REPL
const out_terminal = REPL.Terminals.TerminalBuffer(output)
const basic_repl = REPL.BasicREPL(out_terminal)
const basic_display = REPL.REPLDisplay(basic_repl)
Base.pushdisplay(basic_display)

Using it before the previous code will show the plot. Please note that you use pyplot() backend for Plots, whose default is to open a new window and display the plot in the window, but when the julia finish execution in the command line, it will close the plot window. To deal with this, we could wither change ways for the default display, or use another backend to show the plot, for example, plotly() backend will display the plot through html. The complete code may look like following:

const output = IOBuffer()

using REPL

const out_terminal = REPL.Terminals.TerminalBuffer(output)

const basic_repl = REPL.BasicREPL(out_terminal)

const basic_display = REPL.REPLDisplay(basic_repl)

Base.pushdisplay(basic_display)

using Plots, Measures
plotly()

data = [rand(100), rand(100)];

h = histogram(data, layout = 2,
          title = ["Dataset A" "Dataset B"], legend = false,
          ylabel = "ylabel", margin = 5mm)

display(h)