|
VCS
template layout.
There seem to be a very common question as
to how to make your plot look exactly as you want it.
Through
this tip, you can learn how to change layout of your
plot, sizing, axis
labels, text position, fonts, orientation, label position
&
colorbar position.
How to run the tutorial:
Start python or cdat by typing'python' or 'cdat' at a shell
prompt, then copy and paste the text in blue,
the text in red is
what you will see as output.
First of
all, let us
understand what is happening when we plot some data on a vcs canvas.
The data is plotted with a default settings on what is being plotted
and where (like what text appears, where on the canvas, how much space
the plot takes, what are the axis labels and tick marks, where
the
colorbar appears and how it is labeled). All of that is defined in a
default template that is used when plotting a data.
If we want
to change
the plot layout, we should first create a new object, a template, that
we will change to our likening. We can then save this template for
future
use.
Let's grab
some data and plot with a default template
import cdms,sys,cdtime,vcs
f=cdms.open(sys.prefix+'/sample_data/clt.nc')
#
Extract a 3D data set and get a subset of the time dimension
data
= f('clt', longitude=(-180, 180), latitude = (-90., 90.))
v = vcs.init() # Initial VCS canvas
v.plot(data)
# plot data with a default
template
Let's see
what templates are available to us
v.show('template')
#
Show the list of persistent templates.
***********************Template
Names List******* ****
( 1): ASD
ASD1
ASD10
( 4): ASD11
ASD12
ASD13
...
( 91): por_topof3
por_topof3_dud
quick
( 94): top_of2
***********************End Template
Names List********
As you can
see there is a lot of persistent templates (94). We will use the
standard 'ASD' template as a start template, that we will then modify
to suit our needs.
# Create a new template from
the existing 'ASD' template
t_asd
= v.createtemplate( 'new', 'ASD' )
# Plot the data using the
above 'ASD' new template.
v.clear() # clear the canvas first
v.plot(
data, t_asd )
To list
template members, use the following commands:
t_asd.list()
#
list the templates members
t_asd.list('text')
#
list only text members
t_asd.list('xlabels')
# list only xlabels members
t_asd.list('legend')
# list
only legend member
t_asd.list('data')
# list only data
member
t_asd.list('mean')
#
list only mean member and its values
For more
complete list see: Creating and Modifying Templates.
Each of
the template members have the following attributes (among others):
- priority
(value 1/0) - show or suppress a member
- x1,y1
- the position of a member (lower left point)
- x2,y2 - the position - upper right
corner (if a 'box' member)
The position is given by a normalized coordinates [0.,1.] .
Setting
priority to 0 will cause the member to not be plotted.
The following plot is showing the
names, positions and values of most important member for that template.
Click on the plot to see a bigger picture

By default when we change a
member attribute, the vcs canvas will get updates, to turn it off set :
v.mode=0 #
turn the automatic update off
#For
example, if we want to suppress the 'mean', 'min' and 'max' texts, we
would set their priority to 0 :
t_asd.mean.priority = 0
t_asd.min.priority = 0
t_asd.max.priority = 0
# to move the x-position of a
mean string 0.1 to the right:
xmean_current
= t_asd.mean.x
t_asd.mean.x
= xmean_current + 0.1
t_asd.max.priority
= 1
Let's move
the colorbar, so that it is vertical and displayed on the right side of
the boxfill plot.
The data
member is the area the main plot occupies - the boxfill plot,
the box1 member is the outline of this area.
# to move the right side of a
plot to the left to make space for the legend
# first move the inner data plot
t_asd.data.x2
= 0.87
# then move the surrounding
box i.e the right y-axis
t_asd.box1.x2 = 0.87
# set the top x-axis (second
y axis) to be blank
t_asd.xlabel2.priority = 0
t_asd.xtic2.priority
= 0
# set the right y-axis
(second y axis) to be blank (priority=0)
t_asd.ylabel2.priority = 0
t_asd.ytic2.priority
= 0
# move the colorbar legend
position, to be vertical and to the right
t_asd.legend.x1=0.9
t_asd.legend.y1=0.82
t_asd.legend.x2=0.95
t_asd.legend.y2=0.3
# clear the canvas and plot
the template again
v.clear()
v.plot(
data, t_asd )
Plotting Using Keyword Arguments
The
plot function has many overriding
keyword arguments that control textural and graphical output of the
display. Below is a plot() function showing the uses
of array objects, template object, graphics method object, and key word
arguments. Objects placed in brackets "[ ]" indicate optional entries
into the plot function:
plot(array1, [array2 [,
template [, graphics method [,key=value [,
key=value [, ...]]]]]]),
where array1
and array2
are Numeric arrays
or MA, MV or Python lists; template
represents a template object; graphics
method
represents a graphics method object (such as, boxfill or isofill); and
key=value represents one variable attributes. If no
template is
specified, then the default template will be used. If no graphics
method is specified, then the default boxfill graphics method is used.
Here are some more important keywords:
comment1 = string
#
Comment plotted above file_comment
comment2
= string #
Comment plotted above comment1
comment3
= string #
Comment plotted above comment2
comment4
= string #
Comment plotted above comment3
file_comment
= string #
Comment (defaults to file.comment)
long_name = string
#
Descriptive variable name
name
= string #
Variable name (defaults to var.id)
time
= cdtime #
instance (relative or absolute),
units
= string #
Variable units
[x|y|z|t|w]name = string
#
x or y Dimension name
[x|y|z|t|w]units
= string #
x or y Dimension units
continents =
0,1,2,3,4,5,6,7,8,9,10,11 # if >=1, plot
continents
#
0 signifies "No Continents"
#
1 signifies "Fine Continents"
#
2 signifies "Coarse Continents"
#
3 signifies "United States"
#
4 signifies "Political Borders"
#
5 signifies "Rivers"
See VCS Manual,
Chapter 4 for
extensive list of plot keywords.
Lets use
plot command keywords to override some of the plot attributes:
v.plot(data,t_asd,
comment1='Comment1',
comment2 = 'comment2', comment3 =
'comment3',
comment4='comment4',file_comment='file_comment',
long_name='long_name',name='name',units='units',
xname='xname' )

Check also the following commands, that can move and scale the entire template (those will be presented with details in the next tips)
t_asd.scale(0.75) # scale to 75%
t_asd.move(0.2,'y') # move to the y position of 0.2 in normal units
t_asd.moveto(0.2,0.2)
t_asd.scalefont(0.7)
|