heatmap in Julia : why is there a transpose between the terminal and the IDE? - plot

I'm unsure why the heatmap outputed by the following minimal working example
using Plots
plotlyjs()
List_x = [1, 2, 3]
List_y = [1, 2]
List_f = [0 0 0; 1 2 3]
my_plot = heatmap(List_x, List_y, List_f,
xlabel = "x axis", ylabel = "y_axis")
display(my_plot)
readline()
depends on whether I run the code from my IDE (either VSCode or Atom), or from the terminal with
>>> julia MWE.jl
In the IDE case, I get
Figure produced by the MWE if run from IDE
And in the terminal case I get
Figure produced by the MWE if run from terminal
As you can see, there is a transpose between the two cases. I would like to always have the IDE behavior, can I do something about it?

This behavior was due to a bug reported in https://github.com/JuliaPlots/Plots.jl/issues/3940, fixed in https://github.com/JuliaPlots/Plots.jl/pull/3953, and released in Plots.jl v1.24.2. On the most recent version, the REPL (terminal) plot now looks like:

Related

Plot not showing in 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.
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:
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.
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.)

Creating animated plots in the command line with Julia

Julia has the delightful ability to generate plots constructed from Unicode symbols which are printed directly to the command line in a very straightforward way. For example, the following code generates a Unicode plot of a sine function directly to the command line:
using Plots
unicodeplots();
x = [0:0.1:2*pi;];
y = sin.(x);
plot(x,y)
I would like to try to find a way to create an animated plot of this form directly on the command line. Ideally, I would like to generate a single plot in Unicode that is ``updated" in such a way that it appears animated.
However, although printing hundreds of distinct frames to the command line is naturally less appealing, such a solution is acceptable if it ``looks" like an animation. Another less acceptable solution is to print such Unicode plots into a gif in a way that is consistent for all platforms; attempts to do any of this involving jury-rigging #animate and #gif have largely failed, since either function cannot even print Unicode plots to a file in the Windows form of Julia.
UPDATE: Here is an example of code that generates an "animation" in the command line that is not really acceptable, which simply plots each distinct frame followed by "spacing" in the command line provided by a special Unicode character (tip provided by niczky12):
using Plots
unicodeplots();
n = 100;
x = [0:0.1:4*pi;];
for i = 1:30
y = sin.(x .+ (i/2));
plot(x, y, show=true, xlims = (0,4*pi), ylims = (-1,1))
sleep(0.01)
println("\33[2J")
end
A slight improvement might be this:
let
print("\33[2J")
for i in 1:30
println("\33[H")
y = sin.(x .+ (i/2));
plot(x, y, show=true, xlims = (0,4*pi), ylims = (-1,1))
sleep(0.01)
end
end

R autoplot works when running line-by-line but not when source-ing R script

I'm observing a very strange behaviour with R.
The following code works when I type it in line-by-line into an instance of R run from my terminal. (OS is Debian Linux.)
However it does not work when I try and run source("script.R").
It also does not work from within R Studio.
Specifically, it fails to produce graphical output with autoplot. Writing to pdf file does not work, and if I remove the pdf() and dev.off() lines, no window containing the figure is opened.
Here's a copy of my script...
library(lubridate)
library(ggplot2)
library(matrixStats)
library(forecast)
df_input <- read.csv("postprocessed.csv")
x <- df_input$time
y <- df_input$value
df <- data.frame(x, y)
x <- df$x
y <- df$y
holtmodel <- holt(y)
pdf("autoplot.pdf")
autoplot(holtmodel)
dev.off()
And for convenience, here's a datafile.
"","time","value"
"1",1,2.61066016308988
"2",2,3.41246054742996
"3",3,3.8608767964033
"4",4,4.28686048552237
"5",5,4.4923132964825
"6",6,4.50557049744317
"7",7,4.50944447661246
"8",8,4.51097373134893
"9",9,4.48788748823809
"10",10,4.34603985656981
"11",11,4.28677073671406
"12",12,4.20065901625172
"13",13,4.02514194962519
"14",14,3.91360194972916
"15",15,3.85865748409081
"16",16,3.81318053258601
"17",17,3.70380706527433
"18",18,3.61552922363713
"19",19,3.61405310598722
"20",20,3.64591327503384
"21",21,3.70234435835577
"22",22,3.73503970503372
"23",23,3.81003078640584
"24",24,3.88201196162666
"25",25,3.89872518158949
"26",26,3.97432743542362
"27",27,4.2523675144599
"28",28,4.34654855854847
"29",29,4.49276038902684
"30",30,4.67830892029687
"31",31,4.91896819673664
"32",32,5.04350767355202
"33",33,5.09073406942046
"34",34,5.18510849382162
"35",35,5.18353176529036
"36",36,5.2210776270173
"37",37,5.22643491929207
"38",38,5.11137006553725
"39",39,5.01052467981257
"40",40,5.0361056705898
"41",41,5.18149486951409
"42",42,5.36334869132276
"43",43,5.43053620818444
"44",44,5.60001072279525
Pretty confused because it seems like a trivial script!
change it to:
print(autoplot(holtmodel))
When you step through code, you get an implicit print(...) statement on each code line. When you source() you don't. ggplot (and others!) use print() to trigger their ploting (so that you can conveniently build up a plot step by step without having to wait for flickering figures)

Plot(...) as an output instead of a plot in Julia

with Julia 1.0.3
following this tutorial for plotting with Gadfly (it's from 2015: it might a bit old)
I used the following code:
using RDatasets, Gadfly, Cairo, Plots
sleep = dataset("lme4", "sleepstudy");
plot(sleep, x = "Days", y = "Reaction", Geom.point, Geom.smooth)
and got the following output: Plot(...) instead of a plot. Why am I not seeing a real plot instead
Here is the output of the following command: typeof.(Base.Multimedia.displays):
3-element Array{DataType,1}:
TextDisplay
IJulia.InlineDisplay
Gadfly.GadflyDisplay

How to generate a series of plot with `for`

I learn to program in Julia language.
I want to test which color is better, so I use the following code:
using Plots
x = 1:10; y = rand(10,2);
for i in [0 0.1 0.2]
plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i))
end
How, nothing show.
Can anyone tell me how to generate plot with loop?
P.S.
I have tried another way,
[plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i)) for i in 0:0.1:1]
Nothing happened with above code.
The following code does not work either:
map(i->plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i)), [0 0.1 0.2])
In a script plot is not shown unless you use display function to show it (see section Plotting in scripts here http://docs.juliaplots.org/latest/tutorial/).
You have several options here. This one is the simplest:
for i in [0 0.1 0.2]
display(plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i)))
sleep(1)
end
It will plot your figures in a sequence. I use sleep(1) to make Julia pause between plotting.
Otherwise you can do:
p = [plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i)) for i in 0:0.1:1]
and then plot your figures from Julia REPL like this:
julia> p[5]
will plot fifth figure. Now you do not have to use display because in REPL it will be invoked anyway as I did not use ; at the end of the line. You could write display(p[5]) to get the same effect.
Finally you can consider saving the figures to PNG file and inspecting them later. You can do it either using p array defined above like this:
foreach(i -> savefig(p[i], "fig$i.png"), eachindex(p))
or in a loop like this:
for i in [0 0.1 0.2]
p = plot(x,y, seriestype=:scatter,background_color=RGB(0.4,0.8,i))
savefig(p, "fig$i.png)
end

Resources