PyNGL Home > Functions > Miscellaneous

Ngl.datatondc

Converts coordinates in data space to coordinates in NDC space.

Available in version 1.3.0 or later.

Prototype

x_out, y_out = Ngl.datatondc(plotid, x_in, y_in)

Arguments

plotid

The identifier returned from any PyNGL graphics function that returns a PlotId.

x_in, y_in

One-dimensional (masked) arrays of x and y data coordinates. They must be the same length. For map plots, x_in corresponds to longitude values, and y_in corresponds to latitude values.

Return values

x_out, y_out

One-dimensional (masked) arrays of the same length as x_in and y_in containing the NDC coordinates that the input arrays mapped onto by the coordinate transform function.

Description

The Ngl.datatondc function can be useful when you want to position certain graphic primitives on a plot. See example 1 below.

Coordinate pairs from the input arrays are mapped to the corresponding pairs in the output arrays.

If either input array is a masked array and any of the values are missing, or if any of the output values are out-of-range, then masked arrays are returned with fill values of 1.e30 placed in the corresponding indexes in both output arrays.

See Also

Ngl.ndctodata

Examples

Example 1

This example creates and draws an XY plot, and then shows how to use Ngl.datatondc to convert some data coordinates to NDC coordinates, and use these new coordinates to plot markers.

import Ngl,numpy

#
#  Create some data for xy plots.
#
npts = 500
x = Ngl.fspan(0.,npts-1,npts)
y = 500.+ 0.9 * x * numpy.sin(0.031415926535898*x)

wks = Ngl.open_wks("x11","py_example1")

res = Ngl.Resources()
res.nglMaximize = False 
res.vpXF        = 0.1  
res.vpYF        = 0.9
res.vpHeightF   = 0.8
res.vpWidthF    = 0.8

xy = Ngl.xy(wks,x,y,res)

x_in = x
y_in = numpy.absolute(y)

x_out, y_out = Ngl.datatondc(xy,x_in,y_in)

for i in xrange(len(x_out)):
  print "%4d  data: (%3.0f, %8.4f)  NDC: (%6.4f, %6.4f)" % \
                 (i,x_in[i],y_in[i],x_out[i],y_out[i])

#
#  Draw some markers on the curve in the above plot.
#
Ngl.draw(xy)   #  Redraw the plot to start with.

#
#  Draw markers.
#
gsres = Ngl.Resources()
gsres.gsMarkerColor = "Blue"
gsres.gsMarkerIndex = 14
gsres.gsMarkerSizeF = 10.0
Ngl.polymarker_ndc(wks,x_out[::5],y_out[::5],gsres)

gsres.gsMarkerColor = "Red"
gsres.gsMarkerIndex = 11
Ngl.polymarker_ndc(wks,x_out[3::5],y_out[3::5],gsres)

Ngl.frame(wks)

Ngl.end()

Example 2

This example creates and draws a map plot, and then shows how to use Ngl.datatondc to convert some longitude coordinates along a single latitude value to NDC coordinates. It also uses the reverse function, Ngl.ndctodata, to convert the NDC coordinates back to the original values.

import math,sys
import Ngl,numpy

wks = Ngl.open_wks("x11","ndctodata_0_py")

res = Ngl.Resources()
res.nglMaximize = False
res.vpXF        = 0.1  
res.vpYF        = 0.9
res.vpHeightF   = 0.8
res.vpWidthF    = 0.8

map = Ngl.map(wks,"CylindricalEquidistant",res)

npts = 10
x_in = Ngl.fspan(-180.,180.,npts)
y_in = 30.*numpy.ones(npts,'f')

x_out,y_out = Ngl.datatondc(map,x_in,y_in)
for i in xrange(len(x_out)):
  print "%4d  data: (%6.1f, %3.1f)  NDC: (%8.6f, %8.6f)" % \
               (i,x_in[i],y_in[i],x_out[i],y_out[i])

print "ndctodata"
x_out2,y_out2 = Ngl.ndctodata(map,x_out,y_out)

for i in xrange(len(x_out2)):
   print "%4d  data: (%6.1f, %3.1f)  original data: (%6.1f, %3.1f)" % \
               (i,x_in[i],y_in[i],x_out2[i],y_out2[i])

Ngl.end()

The output should look like:

   0  data: (-180.0, 30.0)  NDC: (0.100000, 0.566667)
   1  data: (-140.0, 30.0)  NDC: (0.188889, 0.566667)
   2  data: (-100.0, 30.0)  NDC: (0.277778, 0.566667)
   3  data: ( -60.0, 30.0)  NDC: (0.366667, 0.566667)
   4  data: ( -20.0, 30.0)  NDC: (0.455556, 0.566667)
   5  data: (  20.0, 30.0)  NDC: (0.544444, 0.566667)
   6  data: (  60.0, 30.0)  NDC: (0.633333, 0.566667)
   7  data: ( 100.0, 30.0)  NDC: (0.722222, 0.566667)
   8  data: ( 140.0, 30.0)  NDC: (0.811111, 0.566667)
   9  data: ( 180.0, 30.0)  NDC: (0.900000, 0.566667)
ndctodata
   0  data: (-180.0, 30.0)  original data: (-180.0, 30.0)
   1  data: (-140.0, 30.0)  original data: (-140.0, 30.0)
   2  data: (-100.0, 30.0)  original data: (-100.0, 30.0)
   3  data: ( -60.0, 30.0)  original data: ( -60.0, 30.0)
   4  data: ( -20.0, 30.0)  original data: ( -20.0, 30.0)
   5  data: (  20.0, 30.0)  original data: (  20.0, 30.0)
   6  data: (  60.0, 30.0)  original data: (  60.0, 30.0)
   7  data: ( 100.0, 30.0)  original data: ( 100.0, 30.0)
   8  data: ( 140.0, 30.0)  original data: ( 140.0, 30.0)
   9  data: ( 180.0, 30.0)  original data: ( 180.0, 30.0)