PyNGL and PyNIO graphics exercises


Basic graphical exercises

The basic_ex.py script generates dummy data to draw a simple line plot (also known as an "XY plot"). Use this script for to start 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. Note how the image in the previous example is rotated ("landscape"). (The reason for this will be covered in the next exercise.) Using this script, force the orientation of the plot to be right side up ("portrait").
    [Hint] [Answer] [Image]

  3. Using the basic_ex.py script, set the nglMaximize resource to False to see what this does to the plot.
    [Hint] [Answer] [Image]

  4. Modify the basic_ex.py 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 nglFrame 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 nglFrame resource to False for both plots so they are both drawn in the same frame. Draw the two plots in different locations on the frame so they don't overlap.

    In order for a valid graphical image to be created, the frame needs to be advanced manually (since we turned off the automatic frame advance), so after both plots are drawn, add a frame advance call.
    [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.py 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 the letters are right side up.
    [Hint] [Answer] [Image]


XY plot exercises (set 1)

Use the xy_ex.py 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 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 Ngl.new_marker 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.py XY plot script to add an X array with 500 points that goes from -100 to 100. Change the Ngl.y call to Ngl.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. Modify the script from the previous exercise so the curves have different line thicknesses and colors. Also, use a special "ngl" resource to point the tickmarks outward.
    [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 a legend to the XY plot.
    [Hint] [Answer] [Image]

  6. Note the strange labels in the legend. These are default labels used by PyNGL.

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


Contour plot exercises

Use the contour_ex.py 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. Also, use a special "ngl" resource to point the tickmarks outward.
    [Hint] [Answer] [Image]

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

  3. Remove all resources from the previous script except for the one that points the tickmarks outward, and add new resources to turn on contour full (the full color map will be spanned by default), and turn off contour lines and line labels.
    [Hint] [Answer] [Image]

  4. Using the script from the previous example, add a main title, change the font color, and change the font to times-bold
    [Hint] [Answer] [Image]

  5. Using the script from the previous example, make the labelbar horizontal, and turn off the labelbar box lines.
    [Hint] [Answer] [Image]

  6. Using the script from the previous example, change the major tickmark spacing on the Y axis to 5, turn off the top tickmarks, 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]

  7. Using the script from the previous example, make the labelbar width slightly longer, the height slightly shorter, and move the labelbar closer to the plot.
    [Hint] [Answer] [Image]


Contours over maps exercises (set 1)

  1. Change the contourmap_ex.py script so that the contours are overlaid on a cylindrical equidistant map. You will notice that the contours are overlaid on a small portion of the map. We'll fix this in the next exercise.
    [Hint] [Answer] [Image]

  2. Set two scalar field resources in order to get the contours overlaid correctly on the map.
    [Hint] [Answer] [Image]

  3. Using what you learned from the contour plot exercises, edit the script from the previous exercise to do the following:
    • Manually set the contour levels to go from 195 to 328 in steps of 5.
    • Turn on contour fill, and turn off contour lines and line labels.
    [Hint] [Answer script] [Image]

  4. Using the script from the previous exercise, change the labelbar orientation to horizontal, remove the labelbar box lines, and move the labelbar closer to the plot.
    [Hint] [Answer script] [Image]

  5. Using the script from the previous exercise, make it so there are fewer labelbar labels. This will cause the labels to get bigger, so resize them to make them smaller.
    [Hint] [Answer script] [Image]

  6. Using the script from the previous exercise, set some map resources to turn off the lat/lon grid lines, turn on map fill such that the oceans and major inland water areas are filled in with "tan", and the land is not filled at all.
    [Hint] [Answer script] [Image]

Contours over maps exercises (set 2)

  1. Run the contourmap2_ex.py and notice the output from the "print" of the file. There are variables "t", "lat", and "lon" on the file. The lat, lon arrays are not coordinate arrays of "t", but we happen to know that they are indeed supposed to be coordinate arrays.

    Use the lat, lon arrays to correctly overlay the contours on a map. Note that these lat, lon arrays only cover the area over the United State. We'll deal with this in the next exercise.

    Note that "t" contains missing value and that a resource is being set to avoid plotting these values.

    Set the main title to "January 1996 Snow Storm".

    [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) turn off the contour lines and labels, 4) set the contour level spacing to 2.25, 5) make the labelbar horizontal, and 6) change the labelbar labels to label only every 5th box.
    [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, set some map resources to change the draw order of the grid lines to "predraw", and to set a dash pattern, color, thickness, and spacing for the grid lines.

    [Hint] [Answer] [Image]


Vector plot exercises

Use the vector_ex.py 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). Point the tickmarks outward.
    [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 of the plot.
    [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".
    [Hint] [Answer] [Image]

  6. Set some vector field resources to set the lat, lon arrays. See what this does to your X/Y axes values.

    Make the labelbar bar thinner and the labels smaller.

    [Hint] [Answer]

  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.py 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 Ng.new_marker function and selecting markers 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 exercise. Instead of using Ngl.polymarker, use Ngl.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 Ngl.panel.

    [Hint] [Answer] [Image]

Primitives exercises (set 2)

  1. Using the poly2_ex.py script that draws a zoomed in view 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 "firebrick" (named color) filled polygon.
    [Hint] [Answer] [Image]

  3. Note that the polygon in the previous example has the map outlines drawn on top of it. Using this script, add two map resources so that the map outlines and grid lines are masked by the polygon fill.

    [Hint] [Answer] [Image]

  4. 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]

  5. 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]

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

    [Hint] [Answer] [Image]

Primitives exercises (set 3)

  1. Using the poly3_ex.py script that draws a simple XY plot, change each Y value to be a bar that goes from the bottom X axis to the Y value. The bar should straddle the X value that it is associated with, and be a width of 30% of the distance between the X values.

    The best way to do this is to loop through each Y value, and call Ngl.xy for each bar. Use a function to return the X,Y coordinates of each bar.

    [Hint] [Answer] [Image]

  2. Using the script from the previous exercise, remove the tickmarks and their labels for the bottom, top, and right axes.

    [Hint] [Answer] [Image]

  3. Using the script from the previous exercise, define a new color map using the named colors: ["white", "black", "FireBrick", "Red", "Orange", "Green", "Navy", "Blue", "SkyBlue","SlateBlue"]. Fill each bar in a different color, starting with "FireBrick".

    [Hint] [Answer] [Image]

  4. Using the script from the previous exercise, move and resize the plot so that the upper left corner is at y=0.95, the width is 0.70, and the height is 0.80.

    [Hint] [Answer] [Image]

  5. To get ready for adding some text and filled boxes outside the plot area, add a call Ngl.draw_ndc_grid to the previous script to draw an NDC grid.

    [Hint] [Answer] [Image]

  6. Using the script from the previous exercise, add a box outline (in color "firebrick" and 3 times as thick as the default) with an upper left corner of (0.2,0.12) and a width of 0.07 and a height of 0.05.

    [Hint] [Answer] [Image]

  7. Using the script from the previous exercise, replace the single box outline with eight box outlines with the same width and height, and with upper left corners of:

    xpos = [0.20,0.40,0.60,0.80,0.20, 0.40, 0.60, 0.80]
    ypos = [0.13,0.13,0.13,0.13,0.055,0.055,0.055,0.055]
    

    Outline each box in a different color, starting with "firebrick".

    [Hint] [Answer] [Image]

  8. Using the script from the previous exercise, add a main title of "Estimated Cancer Deaths for 2002". You will need to slightly decrease the value of vpYF and vpHeightF to make room for the title.

    Also add text strings ["Lung", "Colon", "Breast", "Prostate", "Pancreas", "Lymphoma", "Leukemias", "Ovary"] to the right of each box.

    [Hint] [Answer] [Image]

  9. Using the script from the previous exercise, fill each box in the same color used for the lines, and then outline the box in black. Also outline each bar in the plot in black.

    [Hint] [Answer] [Image]

  10. Using the script from the previous exercise, make the Y tickmarks length 0 (effectively removing them), and draw horizontal dashed grid lines at Y major tickmark locations. Remove the call to Ngl.draw_ndc_grid.

    [Hint] [Answer] [Image]

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.py script that draws a contour plot using dummy data, create three more individual contour plots, using Ngl.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 figure string to each plot, and to decrease the amount of white space between the two rows of plots.

    [Hint] [Answer] [Image]

  8. Modify the script from the previous example to point the tickmarks outward in the individual plots, and turn off the top and right tickmarks. It will no longer be necessary to use the nglPanel resource you set in the previous script to decrease the white space.

    [Hint] [Answer] [Image]

  9. Modify the script from the previous example to add a bold title at the top of the panelled plots.

    [Hint] [Answer] [Image]