DKRZ NCL notebook example

Title:The NCL viewport
DescriptionShows how to use the viewport resources to resize the plot and position it in the frame
20.07.18kmf

First, we define the graphics output format which should be PNG and of size 300x300 pixels.


In [1]:
wks_type = "png"
wks_type@wkWidth  = 300
wks_type@wkHeight = 300
wks = gsn_open_wks(wks_type,"plot_viewport_settings")



If we do not change any default setting for the viewport NCL will center the plot for us.


In [2]:
plot = gsn_csm_map(wks,True)



Viewport edge

Well, we can't see the edge of the viewport but we can show it using the gsn_polyline_ndc function (ndc - normalized device coordinates).
Therefore, we have to assign a variable of type logical and tell NCL not to advance the frame so that the polylines can be added to the plot.


In [3]:
res = True
res@gsnFrame = False



Draw the polyline close to the viewport edge. The viewport is always a square with x: 0.0-1.0 and y: 0.0-1.0.


In [4]:
x = (/0.0001, 0.9999, 0.9999, 0.0001, 0.0001/)
y = (/0.0001, 0.0001, 0.9999, 0.9999, 0.0001/)

gsn_polyline_ndc(wks,x,y,True)



Create the plot again but note that we have to use res instead of the logical True for the plot function.


In [5]:
plot = gsn_csm_map(wks,res)
frame(wks)



Since we are creating a few plots NCL will save each plot output to a separated PNG file named plot_viewport_settings.000001.png, plot_viewport_settings.000002.png, ...

Moving the plot

The next step is to change some default viewport settings to move the plot uppward and slightly to the left. vpXF specifies the location of left edge of the View object's bounding box in NDC (normalized device coordinates) space (default 0.2). And accordingly vpYF specifies the location of top edge of the View object's bounding box in NDC space (default: 0.8).


In [6]:
res@vpXF = 0.05
res@vpYF = 0.99



Let's see what happen.


In [7]:
gsn_polyline_ndc(wks,x,y,True)

plot = gsn_csm_map(wks,res)
frame(wks)



Ok, that was a little bit too much. Let's play with the values.


In [8]:
res@vpXF = 0.07
res@vpYF = 0.98



Create the plot.


In [9]:
gsn_polyline_ndc(wks,x,y,True)

plot = gsn_csm_map(wks,res)
frame(wks)



Better :-)

Changing the plot size

The next step is to change the size of the plot with the viewport resource settings vpWidthF and vpHeightF. vpWidthF specifies the width of View object's bounding box in NDC units (default: 0.6). vpHeightF specifies the height of View object's bounding box in NDC units (default: 0.6).


In [10]:
res@vpWidthF = 0.6
res@vpHeightF = 0.3



Create the plot.


In [11]:
gsn_polyline_ndc(wks,x,y,True)

plot = gsn_csm_map(wks,res)
frame(wks)



Working with two plots

Sometimes we want to display two or more plots in the same frame. Therfore, we can use the gsn_panel function to do it for us but if we want more control about size and position it is better to use the viewport resources.

Draw the polylines, add a title to the plot and draw the first plot.


In [12]:
gsn_polyline_ndc(wks,x,y,True)

res@tiMainString = "First plot"

plot = gsn_csm_map(wks,res)



Create a second plot with its title and draw it below the first one.


In [13]:
res@tiMainString = "Second plot"

res@vpYF = 0.58

plot2 = gsn_csm_map(wks,res)
frame(wks)



Playing with multiple plots

Ok, let's see what we can do with multiple plots using their own resources.

The next part will shortly describe how to handle multiple plots with own resources, different sizes and positions. For a cleaner coding we will define a new graphic output and create new plots.


In [14]:
wks2_type = "png"
wks2_type@wkWidth  = 300
wks2_type@wkHeight = 300
wks2 = gsn_open_wks(wks2_type,"plot_viewport_settings_multiple_plots")



In this example we'll create 4 plots. The common resources for the plots can be copied.


In [15]:
res1 = True
res1@gsnFrame = False

res3 = res1
res4 = res1



Plot 1 and plot 2 should have the same size and be placed on the left side among each other.


In [16]:
res1@vpWidthF = 0.46
res1@vpHeightF = 0.23
res1@vpXF = 0.07

res2 = res1

res1@tiMainString = "First plot"
res1@vpYF = 0.9

res2@tiMainString = "Second plot"
res2@vpYF = 0.5



Plot 3 and plot 4 should be much smaller and be placed on the right side among each other.


In [17]:
res3@vpWidthF = 0.34
res3@vpHeightF = 0.17
res3@vpXF = 0.62

res4 = res3

res3@tiMainString = "Third plot"
res3@vpYF = 0.9

res4@tiMainString = "Fourth plot"
res4@vpYF = 0.6



Now, create all 4 plots. For convenience, they are simple maps again. Draw a line at the edges.


In [18]:
gsn_polyline_ndc(wks2,x,y,True)

plot1 = gsn_csm_map(wks2,res1)
plot2 = gsn_csm_map(wks2,res2)
plot3 = gsn_csm_map(wks2,res3)
plot4 = gsn_csm_map(wks2,res4)

frame(wks2)