How can I get my Python-generated graphics into my problem set write-ups?

The problem: Your Python session has created a graphic in an x11 window, and you'd like to print it or save it so that you can insert it into a word processor document, as part of a report.

Method 1: You can do a "screen grab." Most of the kinds of computers you are likely to be using as a workstation can "grab" an image of any selected window you see on your screen. The precise way you do this depends on what kind of computer you are sitting at; it is independent of whether you are running the courseware on the workstation itself, or via login to a remote server.

On Mac OSX the screen grab utility is called grab. It is located in the Utilities folder, which is in the Applications folder. To use it, you start it up by double-clicking its icon in the usual way. Then, from the Capture menu of grab, select Window. After that you simply click on the x11 window containing the graphic you want to save. A copy of that window will appear in a new window in grab. From grab, you can then do any of the following:

If you are sitting at a Microsoft Windows machine, there are similar screen grab utilities which can copy the contents of your CygWin x11 window. Check the documentation that came with your particular version of Windows. If your keyboard has a PRINT SCREEN key, then (probably) pressing ALT + PRINT SCREEN will put a copy of the currently selected window into the clipboard. If the Windows machine you are using for a workstation is your own, or if at least you have enough ownership to install new software on it, you can install any of the widely available freeware screen capture utilities. Just googol the words Screen Capture Windows NT and you'll find a good selection.

If you are sitting at a Linux workstation, doing a screen grab is a little more complicated, since the software you need to do a useful screen grab is not part of all Linux distributions, and may not have been installed on the particular machine you are using. There is a standard Linux command, called xwd, which will dump the contents of an x11 window, but it saves the image in a rather arcane format that probably won't be of much use to you unless to convert it to something else. To do the screen capture, first go to any terminal window on the workstation and type the command: xwd -out myImage, where you can replace the name "myImage" with whatever you want to call your image file. This will give you a cursor, and then you mouse whichever window you want to dump. The image file in X Windows Dump format will be placed in your current directory. You can use the command convert or imagemagick to convert this image file to a more useful form, like gif or tiff. Type man convert or man imagemagick for instructions. Note all of these commands should be typed from the Linux prompt, not into the Python interpreter, which won't know what they mean. If you read the instructions on convert carefully, you'll see that you can actually use the command to dump an x11 window directly into the format you want. If neither conversion utility is installed on your workstation, you'll have to ask your sysadmin to install them for you. On the whole,if you're using a Linux workstation, it might be simpler to use Method 2 instead of a screen dump.

Method 2. If you have created your plot using the graphics utilities in the module ClimateUtilities, the graphics commands return a plot object that you can use for further manipulations of the plot, including saving it to disk. For example, if you've created a plot using w = plot(c), you can save the plot into a file by typing w.save('myPlot') into the Python interpreter. This will create a file called myPlot.eps in your current directory. (Of course, you are free to use a diffferent filename). The default (and currently only) file format is Encapsulated Postscript (eps). eps files can be read and further edited by most graphics programs (e.g. Canvas or Illustrator) and can be imported directly into many word processor documents.

Further, at the Linux prompt, you can type the command ps2pdf myPlot.eps to convert the file to an Adobe Acrobat pdf file, which will appear in your current directory as myPlot.pdf. This can be read and printed from Acrobat on any kind of computer, and can also be inserted into many kinds of word processor documents. (This should also work from the command line on an OS X Mac, but if your machine doesn't have ps2pdf installed, try pstopdf instead, which is usually part of the standard installation on a Mac). The Linux utility convert can also turn an eps file into almost any form you might want. If you have moved your eps file to an OS X Mac by one means or another, you can also use the utility GraphicConverter (in the Applications folder) to convert eps files to other forms, either individually or in a batch. The Linux command line utility convert can also be installed on an OS X Mac, whereafter it can be invoked from the command prompt in any terminal window.

Note that when invoking a command line utility like ps2pdf myPlot.eps, you need to be in the same directory as the file you are working on, otherwise the command won't find the file. You are always free to specify the full pathname of the file, e.g. ps2pdf /home/rtp1/PythonWork/myPlot.eps.

You might be running Python on your own machine, or in any event the one you are using for writing the report in which you want to insert your graphics. In that case you can just proceed with your work, once you've saved the graph and converted it into a convenient form. More typically,though, you will be running Python by logging into a remote server through an x11 terminal window. Suppose the remote server is called geoflop.uchicago.edu, and you have saved your plot file in eps form. Your file is now on geoflop, located in whatever directory you started Python from. From the Linux command line on geoflop, you can use convert or ps2pdf to convert the file to another format, but you'll still need to move it to a machine of your own, in order to do further work on it. To do this, follow the instructions under the heading: How do I move a file from one computer to another?


This all seems overly complicated, given that all we are trying to do is save a plot file. A lot of the complication comes from the possibility that you are running Python on a remote Linux server; running this way saves the instructor/sysadmin /student the considerable hassle of installing the software and data files on a whole lot of individual machines, all of which may differ in annoying ways. Part of the complexity comes also from the variety of graphics file formats that one might need to deal with. I am working on two things that should help make things simpler in the future. First, future releases of the graphics routines in ClimateUtilities will have the option of saving directly into pdf or gif format, eliminating the conversion step. Second, I am looking at ways of automating the process of getting the graphics onto the students' computer, by using Python helper scripts. One idea I am toying with is to use Python to automatically build a web page on the server, using all the saved files in a specified directory. The web page would be located in the students' own public_html or www directory on the server, and could be then be accessed by any web browser. In many cases, it would be sufficient to simply provide the instructor with the URL of the web page, for grading purposes. Yet another possibility, at some installations, is to make the student's home directory on the server mountable on Linux or Mac OSX workstations via NFS. If the directory has write-permission, this raises some security issues that are troubling to many sysadmins, but if the directory is mounted read-only, the student can at least move the needed files to a local directory with write-permission using familiar drag-and-drop. Suggestions are welcome.