# Graphing Tips

To include a graph in your writeup, just include an image of it (perhaps a screenshot) in your PDF.

You can make the graphs however you like---including using a spreadsheet, or R, or Matlab.

For Python graphing, usually you would use the `pyplot` module, which provides an interface to `matplotlib`. There's lots of documentation online, but here's a quick example:

    import matplotlib.pyplot as plt
    plt.plot(3,5,'b.')               # a blue dot
    plt.plot(4,-1,'b.')              # another blue dot
    plt.plot([7,8,9],[9,8,7],'rx')   # three red x's
    plt.show()

Alternatively, you may like the simple `xgraph` utility, which is easy to use from the command line. A more powerful choice would be `gnuplot`. Both are described below.

Some of the material below is adapted from Jason Baldridge at UT Austin.

#### Remote Graphics

If you are working on your own Linux machine, you should be able to run graphical programs on one of the ugrad machines if you connect to it using the `-Y` option: e.g., `ssh -Y USERNAME@ugrad12.cs.jhu.edu`. This option allows remote programs on `ugrad12` to open windows on your local X Window desktop and send graphics back to them.

This should also work if you're working on your own Windows machine (from WSL).

Remote graphics over X is painfully slow, however. So you may prefer to work directly on one of the ugrad machines (in Malone 122 / Malone G-61) or install graphing programs on your own machine.

#### xgraph

xgraph provides a simple way to create line graphs and scatter plots. The original version is here and is already installed on the ugrad machines, where you can type `man xgraph` for documentation. Alternatively, I think the version at www.xgraph.org is a later version of the same program and is available as an Ubuntu package called `xgraph`.

The following text file specifies a graph:

>     TitleText: Sample Data

>     "Plot one"
>     1 2
>     2 3
>     3 4
>     4 5
>     5 6

>     "Plot two"
>     1 1
>     2 4
>     3 9
>     4 16
>     5 25

>     "Plot three"
>     1 10
>     2 8
>     3 6
>     4 4
>     5 2

This should be pretty self-explanatory: there are three different relationships being plotted. We can optionally name each graph by putting a string in quotes along with the block giving the data. The first column gives x values, the second gives y values.

If the above text is in a file called `mydata.txt`, you can view the graph with the command

>     xgraph mydata.txt

If the above text is printed by your program `myscript`, you can view the graph with the command

>     myscript | xgraph

Use `xgraph -P` instead of `xgraph` if you just want to plot the points and not connect them with lines. There are other options, such as logarithmic plots.

#### gnuplot

A more powerful alternative is gnuplot. It's available from gnuplot.info or as the Ubuntu package `gnuplot`, and is already installed on the ugrad machines.

Let's start by creating two data files `numbers1.dat` and `numbers2.dat`:

>     1 2
>     2 3
>     3 4
>     4 5
>     5 6

>     1 1
>     2 4
>     3 9
>     4 16
>     5 25

Again, the two columns correspond to x and y coordinates of some points. The following gnuplot commands will plot the data along with a function:

>     set xlabel 'My X-axis label'
>     set ylabel 'My Y-axis label'
>     plot 'numbers1.dat' title 'linear', \
>          'numbers2.dat' title 'squared' with l, \
>          sin(x)**2 title 'sinsquared'

The modifier `with l` says to connect the points with lines.

You can start gnuplot and enter these commands interactively, or you can put the commands in a file `mycommands.txt` and type

>     gnuplot -persist mycommands.txt

Or you can pipe the commands into gnuplot:

>     myscript | gnuplot -persist

The `-persist` option keeps the graph window open after the gnuplot session ends. Alternatively, you can dump the graph to a PNG file (for example) by including these gnuplot commands:

>     set term png
>     set output "myplot.png"

Make sure to use those commands before plotting, or else type `replot` after you use them.

You can then view the PNG file with any of various commands such as `display`, `eog`, or even `firefox`. Running the PNG viewer remotely will be slow, so you may want to pull it down to your local machine first:

> `scp USERNAME@ugrad12.cs.jhu.edu:myplot.png . display myplot.png`

The Windows alternative to `scp` is PSCP.