### 2.11 Plotting

BayES provides functions for producing elementary graphics. The following types of plots are supported:

1. histograms using the hist() function

2. scatter plots (y versus x) using the scatter() function

3. correlograms (acf plots) using the acf() function

4. line plots (y versus x or the values of y versus their row index) using the plot() function

5. kernel density estimates using the kden() function

Section B.16 provides extensive documentation on the plotting functions. The remainder of this section gives a general overview of how plots are handled in BayES and presents some simple examples.

When a plotting function is called in BayES in its simple form, a new ﬁgure window is created and the corresponding plot is drawn within this window. Figure windows are named consecutively as "Figure 1", "Figure 2", etc., and these names can be used to interact with them, for example to close them programmatically or save their plots in any of the supported graphics formats. For example, the statement:

close("Figure 2");

will close the ﬁgure window with "Figure 2" appearing on its title bar and release the memory occupied by this ﬁgure. To prevent extreme use of memory resources for presenting plots, BayES restricts the maximum number of ﬁgure windows that are open simultaneously to 20. This number can be changed using the maxﬁgures() function.

Once a ﬁgure window is closed, its name will be reused. For example, if there are currently two ﬁgure windows open with titles "Figure 1" and "Figure 3" (the user closed the ﬁgure window with title "Figure 2"), the next time a plotting function is used, the title of the new ﬁgure window will be "Figure 2". The statement:

close(all);

closes all currently open ﬁgure windows and releases resources.

The titles of ﬁgure windows can also be used to export the associated plots to the following graphics formats:

1. encapsulated postscript (.eps)

2. portable network format (.png)

3. joint photographic experts group (.jpeg)

This is achieved using the export() function, by passing the name of the ﬁgure window that contains the plot to be exported as the ﬁrst argument of the export() function. Section B.4 provides extensive documentation on the export() function.

The ﬁve basic plotting functions mentioned above diﬀer in the number of numerical arguments they take, but all of them have the following optional arguments:

• "title"
• "xlabel"
• "ylabel"
• "grid"
• "colors"

These arguments, if provided, must be given after the numerical arguments of the respective plotting function, separated by commas.8 The values of the ﬁrst four options should be strings and of the "colors" option a matrix. The three ﬁrst options, as presented above, specify the title of the plot and the labels on the ‘x’ and ‘y’ axes, respectively. The values of the "grid" option must equal to either "on" or "oﬀ", with the ﬁrst value requesting that a grid is plotted on the graph. The last option speciﬁes the colors to be used in the graph and its value must a matrix with three columns and, possibly, multiple rows. The values in each row represent a color in RGB (red-green-blue) format and should be between zero and one. The ﬁrst row speciﬁes the background color of the graph9 and the second the color of the axes and text labels. The remaining rows specify the colors to be used when plotting the data.

BayES provides support for ﬁgure windows which can contain multiple plots. A call to the multiplot() function will initialize a ﬁgure window which can store multiple plots. Subsequent calls to the ﬁve basic plotting functions, accompanied by calls to the subplot() function, can be used to populate the spaces of this window with actual plots. See section B.16 for more details.

Example 2.11 demonstrates how to plot a histogram of a set of values contained in a vector, while example 2.12 shows how to overlay kernel density estimates of the values contained in two vectors. Finally, 2.13 demonstrates how to plot a set of functions. Sample "4$-$Plotting data.bsf", located at "\$BayESHOME/Samples/1$-$GettingStarted" contains some more complete examples of using the plotting functions.

Example 2.11
▼ Input ▼ Output

// Set the seed for the random-number
// generator and draw 500 numbers from
// a Gamma(4,2) distribution
seed(42);
x = gamrnd(4,2,500,1);

// Plot a histogram of the values in x
hist(x, 20,
"title"="Histogram of x",
"grid"="on" );

// Export the graph as eps
export( "Figure 1", "./Hist.eps",
"width"=420, "height"=280 );

Example 2.12
▼ Input ▼ Output

// Draw two sets of 500 numbers from
// a Gamma(4,2) distribution
seed(42);
x = gamrnd(4,2,500,1);
y = gamrnd(4,2,500,1);

// Overlay the kernel density estima-
// tes for the values in x and y
kden( [ x, y ],
"title"="Kernel density estimate",
"grid"="on" );

// Export the graph as eps
export( "Figure 1", "./Kden.eps",
"width"=420, "height"=280 );

Example 2.13
▼ Input ▼ Output

// Define x-axis values
x = range(0.01, 4, 0.05);

// Plot the Gamma pdf with varying
// shape parameter
y1 = gampdf(x, 2, 3);
y2 = gampdf(x, 3, 3);
y3 = gampdf(x, 4, 3);
y4 = gampdf(x, 5, 3);
myPlot = plot( [ y1, y2, y3, y4 ], x,
"title" = "Gamma pdfs",
"xlabel" = "x", "ylabel" = "pdf",
"grid"="on" );

// Export the graph as eps
export( myPlot, "./Gampdf.eps",
"width"=420, "height"=280 );

8Optional arguments passed to the plotting functions can be provided in any order, but always come in pairs (eg. "title"="my title").

9Subplots within graphs must have the same background color. The overall background color in graphs that contain multiple subplots is the background color speciﬁed for the subplot at the upper left corner of the graph.