# Generate Code Method

The Generate Code method generates the code needed to reproduce the contents of a graphics window. This method can be used to generate code after changes have been made interactively in the graphics window, e.g., moving, resizing, or changing properties.

## Example

p = PLOT(/TEST, THICK=3, COLOR='red', LAYOUT=[2,1,1])

b = BARPLOT(/TEST, FILL_COLOR='green', LAYOUT=[2,1,2], /CURRENT)

p.GenerateCode, 'mycode.pro'

## Syntax

graphic.GenerateCode [, Filename] [, /SAVEDATA]

## Arguments

### Filename

The name of the file to which the generated code will be written. If no filename is supplied, the generated code will be written to the console.

## Keywords

### SAVEDATA

Set this keyword to save the data used in the visualizations within the graphics window. If this keyword is set, an IDL save file using the same base name as Filename will be used along with the ".sav" extension. The data file will be saved in the same directory as Filename. Additional lines of code needed to restore the variables will appear at the top of the generated code. If Filename is not set, this keyword will be ignored and the data will not be saved.

## Notes

If SAVEDATA is not set, generic variables will be used in the code as placeholders. In order for the code to run, these variables will either have to be replaced with valid variables, or lines of code defining the variables will have to be added prior to each function call.

Example: If the generated code is as follows:

; Code generated by IDL 8.3

plot0 = PLOT(x, y) ; <- Data required

The variables "`x`" and "`y`" will need to be defined, e.g., by adding the following lines above the plot line:

x = myXdata ; where myXdata is the actual x data to be plotted

y = myYdata ; where myYdata is the actual y data to be plotted

The comment `; <- Data required` will be added to the code to indicate the positions of the variables that need to be defined.

The Rotate method is not currently supported. This can be added manually by inserting a line of code after the desired graphic. As an example, adding the following line after the plot line above will rotate the plot by 45 degrees:

plot0.Rotate, 45

Things that cannot be added via the UI, e.g., a new AXIS, also will not appear in the generated code as the code needed to produce those items will already have had to exist and can be copied from there.

COLORBAR and ELLIPSE are not currently supported for GenerateCode.

## Version History

8.3 | Introduced