Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot not showing in Julia

Tags:

plot

julia

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.

like image 475
rnso Avatar asked Mar 30 '26 14:03

rnso


1 Answers

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.)

like image 191
Sundar R Avatar answered Apr 03 '26 17:04

Sundar R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!