I want to be able to pause my program in order to give a Makie.jl window time to render on the screen so I can see what the changes I am making to it are. How can I do this.
The simple answer is to use sleep(t) where t is the time in seconds you sleep or more formally, the time duration for which you want to block the curent task from running. A fun example that highlights the power of sleep() is as follow:
using Makie
x = range(0, stop = 2pi, length = 80)
f1(x) = sin.(x)
f2(x) = exp.(-x) .* cos.(2pi*x)
y1 = f1(x)
y2 = f2(x)
scene = lines(x, y1, color = :blue)
scatter!(scene, x, y1, color = :red, markersize = 0.1)
lines!(scene, x, y2, color = :black)
scatter!(scene, x, y2, color = :green, marker = :utriangle, markersize = 0.1)
display(scene)
sleep(10)
pop!(scene.plots)
display(scene)
sleep(10)
pop!(scene.plots)
display(scene)
You can see the images above that show how the plot progressively gets undone using pop(). The key idea with respect to sleep() is that if we were not using it (and you can test this on your own by running the code with it removed), the fist and only image shown on the screen will be the final image above because of the render time.
You can see if you run this code that the window renders and then sleeps for 10 seconds (in order to give it time to render) and then uses pop!() to step back through the plot.
Docs for sleep()
This might also be a good reference example for this post.
Related
Two questions in one: Given a line plotted in Julia, how can I
delete it from the plot and legend (without clearing the whole plot)
change its properties (such as color, thickness, opacity)
As a concrete example in the code below, how can I 1. delete previous regression lines OR 2. change their opacity to 0.1?
using Plots; gr()
f = x->.3x+.2
g = x->f(x)+.2*randn()
x = rand(2)
y = g.(x)
plt = scatter(x,y,c=:orange)
plot!(0:.1:1, f, ylim=(0,1), c=:green, alpha=.3, linewidth=10)
anim = Animation()
for i=1:200
r = rand()
x_new, y_new = r, g(r)
push!(plt, x_new, y_new)
push!(x, x_new)
push!(y, y_new)
A = hcat(fill(1., size(x)), x)
coefs = A\y
plot!(0:.1:1, x->coefs[2]*x+coefs[1], c=:blue) # plot new regression line
# 1. delete previous line
# 2. set alpha of previous line to .1
frame(anim)
end
gif(anim, "regression.gif", fps=5)
I tried combinations of delete, pop! and remove but without success.
A related question in Python can be found here: How to remove lines in a Matplotlib plot
Here is a fun and illustrative example of how you can use pop!() to undo plotting in Julia using Makie. Note that you will see this goes back in the reverse order that everything was plotted (think, like adding and removing from a stack), so deleteat!(scene.plots, ind) will still be necessary to remove a plot at a specific index.
using Makie
x = range(0, stop = 2pi, length = 80)
f1(x) = sin.(x)
f2(x) = exp.(-x) .* cos.(2pi*x)
y1 = f1(x)
y2 = f2(x)
scene = lines(x, y1, color = :blue)
scatter!(scene, x, y1, color = :red, markersize = 0.1)
lines!(scene, x, y2, color = :black)
scatter!(scene, x, y2, color = :green, marker = :utriangle, markersize = 0.1)
display(scene)
sleep(10)
pop!(scene.plots)
display(scene)
sleep(10)
pop!(scene.plots)
display(scene)
You can see the images above that show how the plot progressively gets undone using pop(). The key idea with respect to sleep() is that if we were not using it (and you can test this on your own by running the code with it removed), the fist and only image shown on the screen will be the final image above because of the render time.
You can see if you run this code that the window renders and then sleeps for 10 seconds (in order to give it time to render) and then uses pop!() to step back through the plot.
Docs for sleep()
I have to say that I don't know what the formal way is to accomplish them.
There is a cheating method.
plt.series_list stores all the plots (line, scatter...).
If you have 200 lines in the plot, then length(plt.series_list) will be 200.
plt.series_list[1].plotattributes returns a dictionary containing attributes for the first line(or scatter plot, depends on the order).
One of the attributes is :linealpha, and we can use it to modify the transparency of a line or let it disappear.
# your code ...
plot!(0:.1:1, x->coefs[2]*x+coefs[1], c=:blue) # plot new regression line
# modify the alpha value of the previous line
if i > 1
plt.series_list[end-1][:linealpha] = 0.1
end
# make the previous line invisible
if i > 2
plt.series_list[end-2][:linealpha] = 0.0
end
frame(anim)
# your code ...
You cannot do that with the Plots package. Even the "cheating" method in the answer by Pei Huang will end up with the whole frame getting redrawn.
You can do this with Makie, though - in fact the ability to interactively change plots was one of the reasons for creating that package (point 1 here http://makie.juliaplots.org/dev/why-makie.html)
Not sure about the other popular plotting packages for Julia.
In statistics, there is rejection method to generate a random variable.
(but it is not main problem)
I finished the sampling, and want to draw graphics in one screen.
I thought it was an 'add=TRUE' option, and I set it up.
But it showed me a new screen.
I do not know what is wrong.
I'm sorry to post a trivial question, but I have no other place to ask.
nsample=10000
fx = function(x){2*exp(-(x^2)/2)/sqrt(2*pi)}
gx = function(x){exp(-x)}
fxdivgx = function(x){(2*exp(-(x^2)/2)/sqrt(2*pi)/exp(-x))}
grid=seq(0.0,5.0, length.out =1000)
which(fxdivgx(grid)==max(fxdivgx(grid)))
grid[201] #x valuable which fx/gx has maximum value
const = fxdivgx(grid[201])
const
fn = function(x){((2*exp(-(x^2)/2)/sqrt(2*pi))/(exp(-x)*const))}
result2 = list()
result2$candi=rexp(nsample)
result2$targetDen=fn(result2$candi)
result2$keep=ifelse(runif(nsample)<result2$targetDen, TRUE, FALSE)
hist(result2$candi[result2$keep], freq=F, breaks=100)
curve(2*exp(-(x^2)/2)/sqrt(2*pi), add='TRUE', col='red')
This is my code.
When I run this code, the results are printed on different screens.
How do I print two pictures in one screen?
hist(result$candi[result$keep], breaks=100)
curve((2*exp(-(x^2)/2)/sqrt(2*pi)), add=TRUE, col='red')
after plotting these two lines, i got below graphics.
In R, it is possible to hold a device, draw the picture, and then flush the device to render the graphics. This is useful for very complex plots with thousands of data points, color gradients etc since without holding, the device would be refreshed after each plotting operation. It works quite well.
However, once the plot is in place, any window operation such as a resize will cause the plot to be refreshed -- however, this time without holding and flushing the device, but plotting the plot elements one by one and refreshing the display each time. This is extremely annoying.
Clearly, I could call manually dev.hold before resizing the window, but this is not a real solution.
Is there a way of telling R that the device should be put on hold for operations such as resize?
As indicated by Dan Slone and gdkrmr viable option is to use intermediate raster file to plot complex graphics.
The flow is the follows:
Save plot to png file.
Plot the image into the screen device.
After this there will be no problems with refreshing and resizing.
Please see the code below:
# plotting through png
plot.png <- function(x, y) {
require(png)
tmp <- tempfile()
png(tmp, width = 1920, height = 1080)
plot(x, y, type = "l")
dev.off()
ima <- readPNG(tmp)
op <- par(mar = rep(0, 4))
plot(NULL, xlim = c(0, 100), ylim = c(0, 100), xaxs = "i", yaxs = "i")
rasterImage(ima, 0, 0, 100, 100, interpolate = TRUE)
par(op)
unlink(tmp)
}
t <- 1:1e3
x <- t * sin(t)
y <- t * cos(t)
# without buffering
# plot(x, y, type = "l")
# with buffering in high-res PNG-file
plot.png(x, y)
Ouput:
With Gadfly it is possible to combine plots in a grid and save the combined plot in a file:
using Gadfly, Compose
x=1:0.1:5;
grid = Array(Compose.Context, (2, 2));
grid[1,1] = render(plot( x=x, y = x, Geom.line));
grid[1,2] = render(plot( x=x, y = x.^2, Geom.line));
grid[2,1] = render(plot( x=x, y = log(x), Geom.line));
grid[2,2] = render(plot( x=x, y = sqrt(x), Geom.line));
draw(SVG("example.svg", 100mm, 100mm), gridstack(grid));
I wrote a function that creates such a plot and this function creates a file. It is a little bit unintuitive for someone who wants to use this function why exactly this function creates a file, while the results of all the other plotting functions with single plots are displayed directly in the browser.
So, is it possible to call a function (in place of draw?) such that the combined plot defined by the grid is displayed directly in the browser like "normal" plots?
function as_temp_html(gadflyplot)
thefilepath=tempname() * ".html"
write(open(thefilepath,"w"),stringmime("text/html",gadflyplot))
return thefilepath
end
You can open this file in your browser by re-using Gadfly's crossplatform "open_file" method:
function open_file(filename)
if is_apple()
run(`open $(filename)`)
elseif is_linux() || is_bsd()
run(`xdg-open $(filename)`)
elseif is_windows()
run(`$(ENV["COMSPEC"]) /c start $(filename)`)
else
warn("Showing plots is not supported on OS $(string(Compat.KERNEL))")
end
end
EDIT: now the answer uses Gadfly-exclusive code
I'm wondering if anyone knows of a way to export plotly 3d charts as a video (more specifically if this can be done natively or requires bodging)?
Exporting a static image is simple, and exporting the interactive plots is fine for embedding in HTML etc.
Lets say I have a 3D chart that I want so simply rotate slowly, this seems like it could be pretty straight forward if the image can be rotated a given interval, an image taken, rotated further ad infinitum, perhaps in a loop - but I'm wondering if this isn't somehow supported natively?
Anyone know of a good strategy?
Solution ideally for R/RStudio, but since plotly is cross-platform, any solutions considered.
For future reference:
The key to being able to iterate multiple perspectives turns out to lie in the camera control "eye", the plotly help centre pointed me toward this:
https://plot.ly/r/reference/#layout-scene-camera
camera = list(eye = list(x = 1.25, y = 1.25, z = 1.25))) #1.25 is default
This is kind of answered here, though searching for my specific query as above didn't find it:
enter link description here
I've used a for loop in the script to pass iterators to a trigonometric function that plots out a circle for the camera co-ordinates, rendering a new image at each step.
(x,y) = cos(theta) + sin(theta)
The eventual code looked like this:
# assume dataset read in, manipulated and given as a matrix in "matrix"
matrix.list <- list(x = temp, y = scan, z = matrix)
font.pref <- list(size=12, family="Arial, sans-serif", color="black")
x.list <- list(title = "X", titlefont = font.pref)
y.list <- list(title = "Y", titlefont = font.pref)
z.list <- list(title = "Z",titlefont = font.pref)
zoom <- 2
for(i in seq(0,6.3,by=0.1){
# 6.3 is enough for a full 360 rotation
outfile <- paste(file,"plot",i, sep = "_")
graph <- plot_ly(matrix.list, x = temp, y = scan, z = z,
type="surface") %>%
layout(scene=list(xaxis = x.list,
yaxis = y.list,
zaxis = z.list,
camera = list(eye = list(x = cos(i)*zoom, y = sin(i)*zoom, z= 0.25))))
# The above camera parameters should orbit
# horizontally around the chart.
# The multiplier controls how far out from
# from the graph centre the camera is (so
# is functionally a 'zoom' control).
graph
plotly_IMAGE(graph, username="xxx", key="xxx",
out_file = paste(outfile,"png", sep="."))
}
NB Number of files and the resolution of them could end up taking up a fair amount of space.
NB 2 I had forgotten while constructing this that plotly's free API limits you to 50 API calls per day, so if you want to render a video, adjust your frames etc, accordingly...