Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python and rpy2: How do I adjust/clear a graphic during runtime?

Tags:

python

r

rpy2

I'm using rpy2 to do data analysis and plotting in python. It works fine except for the fact that when I draw a plot, it's window hangs around until the program terminates. Is there a way to clear the plot during runtime? Additionally, If I ever resize the window, the plot disappears, but the window remains. When using R interactively, resizing the window simply adjusts the size of my plot. I'd like to have this functionality within python.

For example, were I to run the following code, I would be presented with a simple plot.

import rpy2.robjects as robj

xdata = [2,4,6,8,10,12]
ydata = [1,2,3,4,5,6]
Rxdata = robj.IntVector(xdata)
Rydata = robj.IntVector(ydata)

plot = robj.r.plot
plot(x=Rxdata, y=Rydata, main='title')

however, if I wanted to resize the window, the plot would dissapear, and if I want my code to keep doing other things, this window hangs around. If there is a command that works in R to clear the plot that might work, but I can't seem to find one. Any pointers would be appreciated.

like image 808
Wilduck Avatar asked Apr 15 '26 22:04

Wilduck


1 Answers

Yes, you can use the following commands to control the plot window interatively:

dev.new() # opens a new window, and can control the size
dev.off() # closes the window

As an example, see these questions:

  • Creating a Plot Window of a Particular Size
  • How to change current Plot Window Size (in R)
  • How to separate two plots in R?
like image 97
griffin Avatar answered Apr 17 '26 13:04

griffin



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!