0

I am plotting some graphs using R. When I run the program the plot appears and then quickly disappears. How can I make the plot stay?`

I am running the following code found in Dynamic Time Warping in Python

import numpy as np

import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr

# Set up our R namespaces
R = rpy2.robjects.r
DTW = importr('dtw')

# Generate our data
idx = np.linspace(0, 2*np.pi, 100)
template = np.cos(idx)
query = np.sin(idx) + np.array(R.runif(100))/10

# Calculate the alignment vector and corresponding distance
alignment = R.dtw(query, template, keep=True)4
plot(alignments)
dist = alignment.rx('distance')[0][0]

print(dist)

Basically the main file is in python, I have installed rpy2, i am remotely connecting into a unix machine. Now the plot shows up but immediately disappears. This only happens to R plots. When I run matplotlib plots they stay(not disappear). so I am wondering whether I have to put some line of code to make the plots "stay". For example like the matlab "holdon".

Community
  • 1
  • 1
mathopt
  • 569
  • 1
  • 6
  • 17
  • Please produce a minimal example that demonstrates the problem. Be sure to explain how you're running the code. See here: http://stackoverflow.com/q/5963269 – Matthew Lundberg May 20 '13 at 03:32
  • using `x11()` before plotting in `R` should work. – Nishanth May 20 '13 at 06:12
  • thanks..that gives me a general direction. But what's the code for including x11()? ..........I am asking this because other libraries that I use within the same code (matplotlib) uses the X windowing system and doesn't require any explicit code snippets. – mathopt May 20 '13 at 16:05
  • hmm I found this stackoverflow post very helpful. http://stackoverflow.com/questions/12655771/plots-made-with-rpy-sent-to-x11-suddenly-close – mathopt May 22 '13 at 02:40

1 Answers1

1

One solution would be to wait for the user to type "enter" before the program finishes:

raw_input("Please type enter...")

This is also useful with my Matplotlib plots (instead of using pyplot.show(): this closes all the plots automatically).

PS: I just saw that this was suggested in the link from the comments to the original question. I approve. :)

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260