Reread command makes distorted animation in gnuplot - plot

I am trying to have a live plot of a datafile that is being dynamically updated inside the main program. I am plotting using the following .plt file
set pm3d map
set xrange[ -6.0000000000000000 : 6.0000000000000000 ]
set yrange[ -6.0000000000000000 : 6.0000000000000000 ]
sp'fpf.dat'u 1:2:3 w pm3d
pause 0.1
reread
But on running the program, the animation I am obtaining is getting distorted.
I think the problem is taking place because as the file is being dynamically updated, even before gnuplot is able to generate the full plot using the temporarily stored data, the file is being updated again and again. What is the solution?

First, I suggest not to use reread but instead create a loop that you have more control over.
set pm3d
set view map
while (some-condition) {
sp'fpf.dat'u 1:2:3 w pm3d
pause 0.1
}
Now several options come to mind
1) The program that is creating the data could over-write the previous data in place
rather than creating (or truncating) the file each time.
2) Instead of putting gnuplot in a loop based on refreshing every tenth of a second, use explicit synchronization between the data file creation and the subsequent plot. There are many ways you might do this depending on your environment and your control over the programs involved. For example you might replace the pause statement with a second loop that spins until the data file modification time changes (specific commands available depend on your OS and shell)
oldtime = timestamp
while (timestamp == oldtime) {
timestamp = system( "stat --format=%X file.dat" )
pause 0.1
}
# we exited the previous loop because the file timestamp changed
3) You could try using multiplot mode (set multiplot before starting to loop), so that each new plot is drawn on top of the previous plot rather than replacing it. If your diagnosis is correct that the white sections are due to incomplete data, then instead of a blank area you will see the content of the previous plot.

Related

Save all plots already present in the panel of Rstudio

I've made different plots (more than a hundred) for a project and I haven't capture them on the way (yes it's bad , i know). Now, I need to save them all at once but without running again my script (which takes hours). Is there a way to do so within Rstudio ?
Edit: All the plot are already there and I don't want to run them again.
In RStudio, every session has a temporary directory that can be obtained using tempdir(). Inside that temporary directory, there is another directory that always starts with "rs-graphics" and contains all the plots saved as ".png" files. Therefore, to get the list of ".png" files you can do the following:
plots.dir.path <- list.files(tempdir(), pattern="rs-graphics", full.names = TRUE);
plots.png.paths <- list.files(plots.dir.path, pattern=".png", full.names = TRUE)
Now, you can copy these files to your desired directory, as follows:
file.copy(from=plots.png.paths, to="path_to_your_dir")
Additional feature:
As you will notice, the .png file names are automatically generated (e.g., 0078cb77-02f2-4a16-bf02-0c5c6d8cc8d8.png). So if you want to number the .png files according to their plotting order in RStudio, you may do so as follows:
plots.png.detials <- file.info(plots.png.paths)
plots.png.detials <- plots.png.detials[order(plots.png.detials$mtime),]
sorted.png.names <- gsub(plots.dir.path, "path_to_your_dir", row.names(plots.png.detials), fixed=TRUE)
numbered.png.names <- paste0("path_to_your_dir/", 1:length(sorted.png.names), ".png")
# Rename all the .png files as: 1.png, 2.png, 3.png, and so on.
file.rename(from=sorted.png.names, to=numbered.png.names)
Hope it helps.
Although this discussion has been inactive for a while, there are some persons, like myself, who still come across the same problem, and the other solutions don't really seem to even get what the actual question is.
So, hands on. Your plot history gets saved in a variable called .SavedPlots. You can either access it directly, assign it to another variable in code or do the latter from the plots window.
# ph for plot history
ph <- .SavedPlots
In R 3.4.2, I could index ph to reproduce the corresponding plot in a device. What follows is rather straightforward:
Open a new device (png, jpeg, pdf...).
Reproduce your plot ph[index_of_plot_in_history].
Close the device (or keep plotting if it is a pdf with multiple pages).
Example:
for(i in 1:lastplot) {
png('plotname.png')
print(ph[i])
dev.off()
}
Note: Sometimes this doesn't happen because of poor programming. For instance, I was using the MICE package to impute many datasets with a large number of variables, and plotting as shown in section 4.3 of this paper. Problem was, that only three variables per plot were displayed, and if I used a png device in my code, only the last plot of each dataset would be saved. However, if the plots were printed to a window, all the plots of each dataset would be recorded.
If your plots are 3d, you can take a snapshot of all your plots and save them as a .png file format.
snapshot3d(filename = '../Plots/SnapshotPlots.png', fmt = 'png')
Or else, the best way is to create a multi-paneled plotting window using the par(mfrow) function. Try the following
plotsPath = "../Plots/allPlots.pdf"
pdf(file=plotsPath)
for (x in seq(1,100))
{
par(mfrow = c(2,1))
p1=rnorm(x)
p2=rnorm(x)
plot(p1,p2)
}
dev.off()
You can also use png, bmp, tiff, and jpeg functions instead of pdf. You can read their advantages and disadvantages and choose the one you think is good for your needs.
I am not sure how Rstudio opens the device where the plot are drawn, but I guess it uses dev.new(). In that case one quick way to save all opened graphs is to loop through all the devices and write them using dev.print.
Something like :
lapply(dev.list(),function(d){dev.set(d);dev.print(pdf,file=file.path(folder,paste0("graph_",d,".pdf"))})
where folder is the path of the folder where you want to store your graph (could be for example folder="~" if you are in linux and want to store all your graph in your home folder).
If you enter the following function all that will follow will be save in a document:
pdf("nameofthedocument.pdf")
plot(x~y)
plot(...
dev.off()
You can also use tiff(), jpg()... see ?pdf

Maxima: Method rk and draw3d not working properly

I have been following a basic manual of Maxima, I need to solve a differencial ecuation system.
First of all, I loaded both of the packets, load(dynamics); and load(draw);
In order to obtain the points I have:
solutionsPoints: rk([4-x^2-4*y^2,y^2-x^2+1],[x,y],[-1.25,0.75],[t,0,4,0.02]);
I got all the points needed, now to represent this,
draw3d(points_joined = true, point_type = dot, points(solutionsPoints), terminal = eps);
Maxima in this case returns:
[gr3d(points)]
What should I do to have this representation?
Thanks
I see that you have terminal = eps, therefore draw3d will create a .eps file and it won't display the image. I tried draw3d as you showed it, and it created maxima_out.eps in the current directory, and I can use a viewer (I used evince) to look at it, and it seems OK.
If you cut out the terminal = eps part, it will display the curve immediately, without creating an output file.

R scripting in SPSS Modeler 16: change default "rowCount=1000" for modelerData

When applying R transform Field operation node in SPSS Modeler, for every script, the system will automatically add the following code on the top of my own script to interface with the R Add-on:
while(ibmspsscfdata.HasMoreData()){
modelerDataModel <- ibmspsscfdatamodel.GetDataModel()
modelerData <- ibmspsscfdata.GetData(rowCount=1000,missing=NA,rDate="None",logicalFields=FALSE)
Please note "rowCount=1000". When I process a table with >1000 rows (which is very normal), errors occur.
Looking for a way to change the default setting or any way to help to process table >1000 rows!
I've tried to add this at the beggining of my code and it works just fine:
while(ibmspsscfdata.HasMoreData())
{
modelerData <-rbind(modelerData,ibmspsscfdata.GetData(rowCount=1000,missing=NA,rDate="None",logicalFields=FALSE))
}
Note that you will consume a lot of memory with "big data" and parameters of .GetData() function should be set accordingly to "Read Data Options" in node setting.

Removing the file paths in the autotitle key labels in gnuplot, leaving just the filename

I'm using a for loop in gnuplot to plot multiple files, according to the output of a search command. The plot command puts the whole filename in the key, including the full path. I would like to put only the filename, removing the path of directories. Is that possible?
#!/usr/bin/gnuplot -persist
set terminal pdfcairo enhanced size 10in,7in
wmax_List = "`echo $(ls E0025/4D/Re2000/NS.dat.*[02468]000)`"
plot for [i in wmax_List] i using 1:2 with points title i
In this case, all the files are in the same folder. The title i option puts makes that "E0025/4D/Re2000/" appears in every line of the plot key. I could put the plotting script in that folder, but there are thousands of data files there (I'm actually plotting one every 1000), so I prefer to execute the script in its current position.
So, my question is, is there any way to remove the full path from the key? I tried using find instead of ls to generate the list of strings with just the filenames, but I don't know how to put the full path + the string in the plot command.
Thanks
You have different possibilities:
dirname = 'E0025/4D/Re2000/'
wmax_List = system('ls '.dirname.'NS.dat.*[02468]000')
ind = strlen(dirname)+1
plot for [i in wmax_List] i using 1:2 with points title i[ind:*]
or
plot for [i in wmax_List] i using 1:2 with points title system('basename '.i)

R - Keep log of all plots

I do a lot of data exploration in R and I would like to keep every plot I generate (from the interactive R console). I am thinking of a directory where everything I plot is automatically saved as a time-stamped PDF. I also do not want this to interfere with the normal display of plots.
Is there something that I can add to my ~/.Rprofile that will do this?
The general idea is to write a script generating the plot in order to regenerate it. The ESS documentation (in a README) says it well under 'Philosophies for using ESS':
The source code is real. The objects are realizations of the
source code. Source for EVERY user modified object is placed in a
particular directory or directories, for later editing and
retrieval.
With any editor allows stepwise (or regionwise) execution of commands you can keep track of your work this way.
The best approach is to use a script file (or sweave or knitr file) so that you can just recreate all the graphs when you need them (into a pdf file or other).
But here is the start of an approach that does the basics of what you asked:
savegraphs <- local({i <- 1;
function(){
if(dev.cur()>1){
filename <- sprintf('graphs/SavedPlot%03d.pdf', i)
dev.copy2pdf( file=filename )
i <<- i + 1
}
}
})
setHook('before.plot.new', savegraphs )
setHook('before.grid.newpage', savegraphs )
Now just before you create a new graph the current one will be saved into the graphs folder of the current working folder (make sure that it exists). This means that if you add to a plot (lines, points, abline, etc.) then the annotations will be included. However you will need to run plot.new in order for the last plot to be saved (and if you close the current graphics device without running another plot.new then that last plot will not be saved).
This version will overwrite plots saved from a previous R session in the same working directory. It will also fail if you use something other than base or grid graphics (and maybe even with some complicated plots then). I would not be surprised if there are some extra plots on occasion that show up (when internally a plot is created to get some parameters, then immediatly replaced with the one of interest). There are probably other things that I have overlooked as well, but this might get you started.
you could write your own wrapper functions for your commonly used plot functions. This wrapper function would call both the on-screen display and a timestamped pdf version. You could source() this function in your ~/.Rprofile so that it's available every time you run R.
For latice's xyplot, using the windows device for the on-screen display:
library(lattice)
my.xyplot <- function(...){
dir.create(file.path("~","RPlots"))
my.chart <- xyplot(...)
trellis.device(device="windows",height = 8, width = 8)
print(my.chart)
trellis.device(device = "pdf",
file = file.path("~", "RPlots",
paste("xyplot",format(Sys.time(),"_%Y%m%d_%H-%M-%S"),
".pdf", sep = "")),
paper = "letter", width = 8, height = 8)
print(my.chart)
dev.off()
}
my.data <- data.frame(x=-100:100)
my.data$y <- my.data$x^2
my.xyplot(y~x,data=my.data)
As others have said, you should probably get in the habit of working from an R script, rather than working exclusively from the interactive terminal. If you save your scripts, everything is reproducible and modifiable in the future. Nonetheless, a "log of plots" is an interesting idea.

Resources