I am currently trying to generate a heatmap in python from a text file, using R commands (with rpy2). It works fine in R, but when I take it to python, the Quartz interface displays quickly and then closes. I would like either to be able to save the quartz display to a file, or directly save my heatmap to a file without displaying it.
Here is the code I have been using:
import rpy2.robjects as robjects
robjects.r('''
library("gplots")
data = read.csv("/Users/.../Heatmap_data.txt")
DF = data.frame(data)
MD = data.matrix(DF,rownames.force=NA)
heatmap.2(MD, scale="none", col=redgreen(100), cexRow=0.1, key=FALSE, symkey=FALSE, trace="none", Colv=FALSE)
''')
I'm using python 2.7, on OS X Yosemite. Thank you for any help.
writes to the file
/tmp/out.png
without displaying the image:.
Preventing the displayed image from immediately closing can be done like this:
script.py:
The
raw_input
orinput
call prevents the Python interpreter from exiting, thus allowing the window to stay open, until the user presses Enter.The
ion
function callsrinterface.process_revents()
periodically so the displayed window will react to GUI events such as resizing or being closed.dev.copy(png,'/tmp/out2.png')
saves the already-displayed image to a file.