NCL Home > Training > Workshops

NCL graphics exercises

For use with NCL V6.0.0 or earlier

Note: NCL version 6.1.0 contained a major overhaul of the graphics, and as a result, some graphical scripts may look slightly different (helvetica fonts instead of times-roman, different default color map, "~" is the default function code, etc).

We decided to keep a set of the NCL graphical exercises as they would look with NCL Versions 6.0.0 and earlier. Click here to see the examples with NCL V6.1.0 or later.

The images linked to from this page were generated without the use of a file called .hluresfile, which we normally recommend that you download before you use NCL. We wanted you to see what these graphics look like without a ".hluresfile". With V6.1.0, you may not need the ".hluresfile" unless you want to change some defaults.


Basic graphical exercises

The basic_ex.ncl script generates dummy data to draw a simple line plot (also known as an "XY plot"). Use this script for the following exercises:

  1. The graphical output is currently going to an X11 window. Change the graphical output to go to a PostScript or PDF file called "basic_ex01.ps" or "basic_ex01.pdf".
    [Hint] [Answer] [Image]

  2. Using the script from the previous exercise, force the orientation of the plot to be "landscape" instead of "portrait".
    [Hint] [Answer] [Image]

  3. Using the script from the previous exercise, try setting the gsnMaximize resource to True to see what this does to the plot.
    [Hint] [Answer] [Image]

  4. Modify the basic_ex.ncl script to generate a second set of points using the cos function. Use these points to create a second XY plot.

    Try it first with the output going to an X11 window, and then with the output going to a PostScript file.
    [Hint] [Answer] [Image 1] [Image 2]

  5. Using the script from the previous exercise, set the gsnFrame resource to False for the first plot only, to see what happens to the plots.
    [Hint] [Answer] [Image]

  6. Take the script from the previous exercise and set the gsnFrame resource to False for both plots so they are both drawn in the same frame. Also, draw the two plots in different locations on the frame so you can see both of them. In order for a valid graphical image to be created, the frame needs to be advanced manually, so add a call to do this.
    [Hint] [Answer] [Image]


Color exercises

  1. Write a script to draw the default color table.
    [Hint] [Answer] [Image]

  2. Using the above script you wrote, change the color table to "BlGrYeOrReVi200".
    [Hint] [Answer] [Image]

    Try drawing some other predefined color tables. You can see the full list of available color tables in the color table gallery.

  3. Keeping the "BlGrYeOrReVi200" color table, add gray to it.
    [Hint] [Answer] [Image]

    Try adding other colors, using different RGB triplets.

  4. Create your own color table using RGB triplets.
    [Hint] [Answer] [Image]

  5. Create your own color table using named colors.
    [Hint] [Answer] [Image]


Title exercises

Look at the title_ex.ncl script that sets a main title. Use this script for the following exercises:

  1. Add two title resources to change the font and the color of the title. Try using both a named color and a color map index value.
    [Hint] [Answer] [Image]

  2. Add titles to the X and Y axes, and increase the font height of both.
    [Hint] [Answer] [Image]

  3. Add a Y title to the right axis and change the orientation of the text so it is right side up.
    [Hint] [Answer] [Image]

For more title examples, see the title applications page.


XY plot exercises (set 1)

Use the xy_ex.ncl XY plot script as a base for the following exercises:

  1. Add an xy resource to change the line color. Try using both a named color and then a color map index value.
    [Hint] [Answer] [Image]

  2. Add an "xy" resource to make the line four-and-a-half times as thick.
    [Hint] [Answer] [Image]

  3. Add three "xy" resources to 1) change to "marker" mode, 2) make the marker a filled dot, and 3) change the marker color to "red".
    [Hint] [Answer] [Image]

  4. Using the script from the previous exercise, use NhlNewMarker to define your own XY marker, and use this instead. Subscript the "y" data array so that only every third value is plotted.
    [Hint] [Answer] [Image]

XY plot exercises (set 2)

You will need to download the xy2.txt ASCII file to do these exercises.

  1. Modify the xy2_ex.ncl XY plot script to add an X array with 500 points that goes from -100 to 100. Change the gsn_csm_y call to gsn_csm_xy and pass it this new X array.
    [Hint] [Answer] [Image]

  2. Using the previous script, plot the full "y" array as four separate curves. In order to do this, the array needs to be dimensioned ncurves (4) x npts (500), which means it needs to be reordered first.
    [Hint] [Answer] [Image]

  3. Note how each of the four curves from the previous exercise are different dash patterns. Modify the script so the curves are all solid, but have different line thicknesses and colors. Also, maximize the plot in the frame.
    [Hint] [Answer] [Image]

  4. Using the previous script, change the limits of the X and Y axes so that the X axis goes from min(x) to max(x), and the Y axis goes from -500 to 1100.
    [Hint] [Answer] [Image]

  5. Using the previous script, add an automatic legend to the XY plot.
    [Hint] [Answer] [Image]

  6. Using the script from the previous exercise, change the labels in the legend, and also make the legend box smaller and move it inside the plot.
    [Hint] [Answer] [Image]

    For more legend plot examples, see the legend applications page.

For more XY plot examples, see the XY applications page.


Contour plot exercises

Use the contour_ex.ncl contour plot script as a base for the following examples:

  1. Add some contouring resources to change the contour levels to start at 5 and go to 95 in steps of 5.
    [Hint] [Answer] [Image]

  2. Using the script from the previous example, make each contour line 3 times as thick, and each a different color. Use the "rainbow" color map.
    [Hint] [Answer] [Image]

  3. Using the script from the previous example, turn on contour fill. Make sure you span the full color map.
    [Hint] [Answer] [Image]

  4. Using the script from the previous example, make the labelbar vertical, and fix the stride of the labelbar labels so they are not so close to each other.
    [Hint] [Answer] [Image]

  5. Using the script from the previous example, change the major tickmark spacing on the Y axis to 5, and explicitly label the bottom axis tickmarks at values 5, 10, 20, 25, and 35 with the labels "Jan", "Feb", "Mar", "Apr", and "May".
    [Hint] [Answer] [Image]

There are many contouring examples. To see a list of them, see the main applications page. Here's a list of some of the pages of interest:


Contours over maps exercises (set 1)

  1. Change the contourmap_ex.ncl script so that the contours are overlaid on a cylindrical equidistant map (note: the data already contains the necessary coordinate arrays for overlaying on a map). Ignore the warning messages for now.
    [Hint] [Answer] [Image]

  2. Note that when you run the contourmap_ex01.ncl script, you get several error messages of the form:
    warning:_NhlCreateSplineCoordApprox: Attempt to create spline approximation
    for X axis failed: consider adjusting trXTensionF value
    
    Also note that it suggests you do the following:
    (0)     gsn_add_cyclic: Warning: The range of your longitude coordinate array
            is at least 360.
    (0)     You may want to set gsnAddCyclic to False to avoid a warning
    (0)     message from the spline function.
    
    Try setting this resource as suggested, and rerun the script.
    [Hint] [Answer] [Image]

  3. Using what you learned from the contour plot exercises, edit the script from the previous exercise to do the following:
    • Set the colormap to "rainbow".
    • Manually set the contour levels to go from 195 to 328 in steps of 5.
    • Turn on contour fill and turn off contour lines.
    • Span the full colormap for the contour fill.
    • Fix the labelbar labels so they don't run into each other.
    [Hint] [Answer script] [Image]

  4. Using the script from the previous exercise, change the data's "units" attribute to be "Degrees K", add a left subtitle "Temperature", and turn off the labelbar box lines.
    [Hint] [Answer script] [Image]

Contours over maps exercises (set 2)

    The next series of scripts deal with a netCDF file whose variable does not have coordinate arrays attached to it.

  1. Run the contourmap2_ex.ncl and notice the output from the print of the file, and the printVarSummary of variable "t". This variable has no coordinate information attached to it, but there are latitude and longitude variables on the file.

    Attach the latitude/longitude coordinate information to "t" so that you can overlay the contours on a map. You will also need to turn off the automatic addition of a cyclic point, because this data is not global.

    Note that only a small area of the map has contours over it. We will deal with this in the next exercise.
    [Hint] [Answer] [Image]

  2. Using the script from the previous example, zoom in on the area defined by the latitude/longitude arrays (it's an area that encompasses the United States).
    [Hint] [Answer] [Image]

  3. Using the script from the previous example and resources that you learned from previous examples, 1) change the color map to "temp1", 2) turn on contour fill, 3) span the full color map, 3) turn off the contour lines, 4) fix the spacing of the labelbar labels, and 5) set the contour level spacing to 2.25.
    [Hint] [Answer] [Image]

  4. Using the script from the previous example, change the projection to "LambertConformal". This will require tweaking the center latitude/longitude, and setting the map limit mode.
    [Hint] [Answer] [Image]

  5. Using the script from the previous example, use the gsnLeftString, gsnCenterString, and gsnRightString subtitle resources to create titles of "Temperature", "January 1996 Snow Storm", and "Celsius", respectively. Remove the settig of tiMainString. Also, turn on a latitude/longitude grid and change the dash pattern of the lat/lon lines.
    [Hint] [Answer] [Image]

  6. Using the script from the previous example, set some resources to mask the lat/lon grid lines over land, increase the grid spacing to 10, decrease the grid thickness, and to change their color.
    [Hint] [Answer] [Image]

There are many contouring over map examples. To see a list of them, see the main applications page. Here's a list of some of the pages of interest:


Vector plot exercises

Use the vector_ex.ncl vector plot script as a base for the following examples:

  1. Add a single vector resource to make the vectors multi-colored.
    [Hint] [Answer] [Image]

  2. Using the script from the previous example, set the reference magnitude and increase the length of it.
    [Hint] [Answer] [Image]

  3. Using the script from the previous example, change the color map to "rainbow+gray", and set some resources to start at color index 16, and to not include the last color in the color map (which is gray).
    [Hint] [Answer] [Image]

  4. Set some vector resources to double the thickness of the line arrows, and to move the little reference annotation box to inside the plot area, flush left and bottom. Set a labelbar resource to make the labelbar vertical.
    [Hint] [Answer] [Image]

  5. Set some resources to set the vector levels to a fixed array, and to set the main title to a concatenation of the "reftime" on the file, and the appropriate "timestep". You will also need to change the "function code" character, since the default is a ":", and this title has a ":" in it. (In NCL V6.1.0, the default function code is "~", so you don't need to change the function code.)
    [Hint] [Answer] [Image]

  6. Set a resource to get curly vectors instead of straight vectors. Some people think curly vectors show a lot more information than straight vectors. You can also get wind barbs via the same resource.
    [Hint] [Answer] [Image]

  7. Tweak the previous example to generate a vector plot at each timestep, but skip over the fields that are all missing values in either u or v. Make sure the title changes with each timestep.
    [Hint] [Answer] [Animation]

For more vector plot examples, see the vector applications page.


Primitives exercises (set 1)

  1. Using the poly_ex.ncl script that draws a single-curve XY plot, add a marker (also called a "polymarker") at the point (-20,-300). To do this, you will need to turn off the automatic frame advance so the page won't advance before you draw the marker.
    [Hint] [Answer] [Image]

  2. Note that it is hard to see the marker from the previous exercise. Using this same script, change the marker to a purple filled dot that is larger than the default size.
    [Hint] [Answer] [Image]

  3. Use the script from the previous exerise. Create three new markers using the NhlNewMarker function and selecting a marker from the weather2 font table.

    Draw three groups of markers using these new markers, each one a different color and size. Also, draw multiple markers for each group.

    [Hint] [Answer] [Image]

  4. Use the script from the previous exerise. Instead of using gsn_polymarker, use gsn_add_polymarker to attach the markers to the plot.

    When you do this, the markers become part of the plot, and won't be drawn until the plot is drawn. This is useful if you later plan to resize the plot, or pass a series of these plots to gsn_panel.

    [Hint] [Answer] [Image]

Primitives exercises (set 2)

  1. Using the poly2_ex.ncl script that draws part of a cylindrical equidistant map, add some lines (also called "polylines") to make a box at the lat/lon coordinates (30,-90), (30,-45), (0,-45), and (0,-90). Make the lines red and twice as thick as the default.
    [Hint] [Answer] [Image]

  2. Using the same code from the previous example, change the lines to a brown filled polygon.
    [Hint] [Answer] [Image]

  3. Use the script from the previous exerise. Change the solid fill to a purple pattern fill, and increase the density of the fill pattern.

    [Hint] [Answer] [Image]

  4. Using the previous exercise and the one from example 1, draw the polygon and then outline the polygon with lines. Add markers at all four corner of the boxes. Try playing with "gs" resources to change things like color, sizes, thickness, and fill patterns.

    [Hint] [Answer] [Image]

  5. Using the script from the previous exercise, add a text box in the middle of the polygon.

    [Hint] [Answer] [Image]

Primitives exercises (set 3)

This suite of exercises shows how to annotate plots with polylines, polygons, and text outside the plot area.
  1. Using the poly3_ex.ncl script that draws a bar chart (see image), remove the bottom axis tickmark and labels. This is in preparation for adding a custom legend.

    [Hint] [Answer] [Image]

  2. Call the special drawNDCGrid procedure to draw an NDC grid. This is a very handy procedure to use when selecting NDC values of where to put boxes and text outside any plot.

    [Hint] [Answer] [Image]

  3. Using the script from the previous exercise and the NDC grid to guide you, add a firebrick-colored box outline at NDC corners (0.2,0.1), (0.27,0.1), (0.27,0.15), and (0.2,0.15).

    [Hint] [Answer] [Image]

  4. Using the script from the previous exercise, add seven more boxes of equal size outside the plot area. Draw four of them using the same Y position, and then draw four more under the first four, using the same X positions. Start the first box at X position 0.15 instead of 0.20.

    Make the box outlines three times as thick, and use the same colors that are used for the bars.

    [Hint] [Answer] [Image]

  5. Using the script from the previous exercise, add text strings to the right side of each box. The original text strings used on the bottom X axis are a bit long, so create a new array of strings that contain shorter labels (like "Lymphoma" instead of "Non-Hodgkin's Lymphoma"). You will need to make the size of the text font smaller.

    [Hint] [Answer] [Image]

  6. Using the script from the previous exercise, change the box to be filled, using the same color as the outline; change the outline to black. Remove the code that sets the line thickness so we're using the default.

    [Hint] [Answer] [Image]

  7. Using the script from the previous exercise, move the bar plot slightly up and to the left in order to make more room, and then make it slightly wider and higher so that it uses more of the NDC unit square. You can use the NDC grid lines for guidance.

    Once you have the plot moved and resized, clean up the script by removing the call to drawNDCGrid.

    [Hint] [Answer] [Image]

For more polymarker, polyline, and polygon examples, see the primitives applications page.

Paneling exercises

This suite of exercises shows how to put multiple plots (of the same size) on a single frame.
  1. Using the panel_ex.ncl script that draws a contour plot using dummy data, create three more individual contour plots, using generate_2d_array to generate more (unique) dummy data.

    [Hint] [Answer] [Image] (only first image shown)

  2. Modify the script from the previous example to panel the four plots into 2 rows and 2 columns in one frame.

    [Hint] [Answer] [Image] (only last image shown)

  3. Modify the script from the previous example to turn off the drawing of the individual plots (leave the paneled plots as is).

    [Hint] [Answer] [Image]

  4. The labelbars in the previous script all have different ranges. Modify the script from the previous example to make the labelbars use the same range, by setting a minimum and maximum contour level, and a spacing.

    [Hint] [Answer] [Image]

  5. Modify the script from the previous example to set the contour levels via an explicit array of levels that are not equally spaced.

    [Hint] [Answer] [Image]

  6. Modify the script from the previous example to turn off the individual labelbars for each plot, and then set a common labelbar in the paneled plots.

    [Hint] [Answer] [Image]

  7. Modify the script from the previous example to add a single title to the paneled plots.

    [Hint] [Answer] [Image]

  8. Modify the script from the previous example to add a single figure string to each plot, and to decrease the amount of white space between the two rows of plots.

    [Hint] [Answer] [Image]

  9. Modify the script from the previous example to turn off the top and right tickmarks. It will no longer be necessary to use the gsnPanel resource you set in the previous script to decrease the white space.

    [Hint] [Answer] [Image]