2

I have a file named mycode.jl with following code taken from here.

using MultivariateStats, RDatasets, Plots

# load iris dataset
println("loading iris dataset:")
iris = dataset("datasets", "iris")
println(iris)
println("loaded; splitting dataset: ")

# split half to training set
Xtr = Matrix(iris[1:2:end,1:4])'
Xtr_labels = Vector(iris[1:2:end,5])

# split other half to testing set
Xte = Matrix(iris[2:2:end,1:4])'
Xte_labels = Vector(iris[2:2:end,5])

print("split; Performing PCA: ")


# Suppose Xtr and Xte are training and testing data matrix, with each observation in a column. We train a PCA model, allowing up to 3 dimensions:
M = fit(PCA, Xtr; maxoutdim=3)
println(M)

# Then, apply PCA model to the testing set
Yte = predict(M, Xte)
println(Yte)


# And, reconstruct testing observations (approximately) to the original space
Xr = reconstruct(M, Yte)
println(Xr)

# Now, we group results by testing set labels for color coding and visualize first 3 principal components in 3D plot
println("Plotting fn:")
setosa = Yte[:,Xte_labels.=="setosa"]
versicolor = Yte[:,Xte_labels.=="versicolor"]
virginica = Yte[:,Xte_labels.=="virginica"]

p = scatter(setosa[1,:],setosa[2,:],setosa[3,:],marker=:circle,linewidth=0)
scatter!(versicolor[1,:],versicolor[2,:],versicolor[3,:],marker=:circle,linewidth=0)
scatter!(virginica[1,:],virginica[2,:],virginica[3,:],marker=:circle,linewidth=0)
plot!(p,xlabel="PC1",ylabel="PC2",zlabel="PC3")

println("Reached end of program.")

I run above code with command on Linux terminal: julia mycode.jl

The code runs all right and reaches the end but the plot does not appear.

Where is the problem and how can it be solved.

rnso
  • 23,686
  • 25
  • 112
  • 234

1 Answers1

6

As the Output section of the Plots docs says:

A Plot is only displayed when returned (a semicolon will suppress the return), or if explicitly displayed with display(plt), gui(), or by adding show = true to your plot command.

You can have MATLAB-like interactive behavior by setting the default value: default(show = true)

The first part about "when returned" is about when you call plot from the REPL (or Jupyter, etc.), and doesn't apply here.

Here, you can use one of the other options:

  • calling display(p) after the last plot! call (this is the most common way to do it)
  • calling gui() after the last plot!
  • adding a show = true argument to the last plot! call
  • setting the default to always show the plot by setting Plots.default(show = true) at the beginning of the script

Any one of these is sufficient to make the plot window appear.


The plot closes when the Julia process ends, if that's happening too soon, you can either:

  1. Run your code as julia -i mycode.jl at the terminal - this will run your code, display the plot, and then land you at the Julia REPL. This will both keep the plot open, and let you work with the variables in your code further if you need to.
  2. add a readline() call at the end of your program. This will keep Julia waiting for an extra press of newline/Enter/Return key, and the plot will remain in display until you press that.

(Credit to ffevotte on Julia Discourse for these suggestions.)

Sundar R
  • 13,776
  • 6
  • 49
  • 76
  • The graph appears but immediately the program ends. How can I make the graph stay on screen? – rnso Sep 04 '22 at 12:56
  • I've edited the answer in response. – Sundar R Sep 04 '22 at 13:28
  • It's weird that one needs to add the boilerplate code `display(p)` and `readline()` and that this is not just the default behaviour... especially when it is not mentioned in the documentation, as if everyone just worked in a notebook... – exchange Aug 10 '23 at 10:39
  • @exchange The `display(p)` being needed is mentioned in the documention, and the quote at the beginning of this answer is from the docs. It might be nice to add something about `readline` too, somewhere in that page. It's probably not been added yet because most people do work with Julia in either notebooks, terminal REPLs or REPLs within VS Code. This kind of script invocation is not the typical pattern (but is maybe slowly becoming more common, after many ways to avoid the usual latencies of this method have become available). – Sundar R Aug 10 '23 at 20:27