#! /opt/local/bin/python

""" Demonstrate plplot sub pages.

    Do not use plenv() - use plenv0()!
    plenv() advances the sub-pages, which we do *not* want here.

"""

from plplot import *

nx = 2
ny = 3
np = nx * ny

def initialize(dev):
    if dev == 'xwin':
        plsdev('xwin')
    else:
        plsdev('ps')
        plsfnam('temp.ps')
    plinit()
    plsdiori(1)
    plssub(nx, ny)

def setup(i):
    print 'setting up plot: ', i
    pladv(i)
    plenv0(0.0, 1.0, 0.0, 1.0, 1, -1)
    plptex(0.5,0.5,0.0,0.0,0.5,'Sub-page: {}'.format(i))

def annotate(i):
    pladv(i)
    print 'drawing in plot: ', i
    plptex(0.2,0.2,0.0,0.0,0,'Drawing, page: {}'.format(i))


def main():

    # initialize
    dev = raw_input(' xwin or ps? ')
    initialize(dev)

    # set up all six plots, from number 1 to 6.
    for i in range(nx*ny):
        setup(i+1)

    # draw in a specific subpage - no output!
    pladv(2)
    plptex(0.2,0.2,0.0,0.0,0,'Some text')

    # now write in all of the plots, backwards from 6 to 1
    for i in range(np, 0, -1):
        annotate(i)

# this is needed, or all of the last plotted stuff does not come out.
    plend()

main() 
