PyNGL is a Python language module for creating 2D high performance visualizations of scientific data. It is based on NCL graphics but still not as extensive as NCL's last version 6.6.2.
The aim of this notebook is to give you an introduction to PyNGL, read your data from file, create plots, and write the graphics output to a specified graphics file format.
Content
The Python module of PyNGL is called Ngl.
In [1]:
import Ngl
To create a visualization of your data you need to do
How to read the data has been explained in 03_Xarray_PyNIO_basics, we will use it here without further explainations.
Let us start opening a graphics output channel and link it to the variable wks. You can call it like ever you want but it is used very often by NCL users.
The workstation types are
In our first example we want to use PNG as output format to make it possible to display the plots in the notebook. To open a workstation we use the function Ngl.open_wks. The name of the graphics output file shall be plot_test1.png. The suffix .png will be appended automatically to the basename of the file name.
In [2]:
wks = Ngl.open_wks('png', 'plot_test1')
That is of course a very simple case but if you want to specify the size or orientation of the graphics you have to work with resources. NCL users already know how to deal with resources, and it shouldn't be difficult to Python users. Resources are the same as attributes of Python objects, if set the user is able to manage a lot of settings for PyNGL functions.
Let us say, we want to generate a PDF file of size DIN A4. First, we have to assign a PyNGL object variable wks_res (you can call it like you want) with the function Ngl.Resources() to store the size settings for the workstation. Notice, that we now use Ngl.open_wks with three parameters, and we have to delete the first workstation.
In [3]:
wks_res = Ngl.Resources()
wks_res.wkPaperSize = 'A4'
wks = Ngl.open_wks('pdf', 'plot_test_A4', wks_res)
There are many wk resources available (see NCL's wk resources page). Read the resources manual carefully because PyNGL and NCL make a lot of differences depending on the selected output format.
The next example shows how to set the size of the output to legal giving the width and height in inches instead of wkPaperSize = 'legal'. It will create a PDF file with width 8.5 inches, height 14.0 inches, and the orientation is portrait (default).
In [4]:
wks_res = Ngl.Resources()
wks_res.wkPaperWidthF = 8.5 # in inches
wks_res.wkPaperHeightF = 14.0 # in inches
wks = Ngl.open_wks('pdf',' plot_test_legal', wks_res)
Now, we want to change the orientation of the legal size PDF file to landscape.
In [5]:
wks_res = Ngl.Resources()
wks_res.wkPaperSize = 'legal'
wks_res.wkOrientation = 'landscape'
wks = Ngl.open_wks('pdf', 'plot_test_legal_landscape', wks_res)
Ok, we want to start with a clean script. We delete the workstation from above using the function Ngl.delete_wks.
In [6]:
Ngl.delete_wks(wks)
In [ ]: