Skip to content

Fix contour plot error highlighted by ORCA2 grid example #83

Description

@sadielbartholomew

Investigate and fix test failure whereby an IndexError can appear to prevent a contour plot being made from code which should work, specifically as per the (just refreeshed) docs example:

======================================================================
ERROR: test_example_unstructured_orca_1 (__main__.UnnumberedExamplesTest.test_example_unstructured_orca_1)
Test example for unstructured grids: ORCA grid example 1.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/slb93/git-repos/cf-plot/cfplot/test/test_examples.py", line 1422, in test_example_unstructured_orca_1
    cfp.con(x=lons, y=lats, f=temp, ptype=1)
  File "/home/slb93/git-repos/cf-plot/cfplot/cfplot.py", line 3303, in con
    _cf_data_assign(f, colorbar_title, verbose=verbose)
  File "/home/slb93/git-repos/cf-plot/cfplot/cfplot.py", line 1379, in _cf_data_assign
    myz = find_z(f)
          ^^^^^^^^^
  File "/home/slb93/git-repos/cf-plot/cfplot/cfplot.py", line 10752, in find_z
    mycoords = find_dim_names(f)
               ^^^^^^^^^^^^^^^^^
  File "/home/slb93/git-repos/cf-plot/cfplot/cfplot.py", line 10724, in find_dim_names
    if field.coord(coords[i]).X:
                   ~~~~~~^^^
IndexError: list index out of range

Activity

  1. sadielbartholomew commented on Apr 14, 2025

    @sadielbartholomew
    MemberAuthor

    In #97, a patch was included after the issue was identified as the requirement for masking invalid values else the contour plot call falls over, as follows, though this should be moved into the code and handled under-the-hood and not with a call to np.ma.masked_invalid in the snippet as is the present workaround:

    # Mask NaN else the plot will fail with:
    # Traceback (most recent call last):
    #   File "/home/slb93/git-repos/cf-plot/cfplot/test/test_examples.py", line 1030, in test_example_unstructured_orca_1
    #     cfp.con(f=temp, x=lons.array, y=lats.array, ptype=1)
    #   File "/home/slb93/git-repos/cf-plot/cfplot/cfplot.py", line 3297, in con
    #     _cf_data_assign(f, colorbar_title, verbose=verbose)
    #   File "/home/slb93/git-repos/cf-plot/cfplot/cfplot.py", line 1379, in _cf_data_assign
    #     myz = find_z(f)
    #           ^^^^^^^^^
    #   File "/home/slb93/git-repos/cf-plot/cfplot/cfplot.py", line 10748, in find_z
    #     mycoords = find_dim_names(f)
    #                ^^^^^^^^^^^^^^^^^
    #   File "/home/slb93/git-repos/cf-plot/cfplot/cfplot.py", line 10720, in find_dim_names
    #     if field.coord(coords[i]).X:
    #                    ~~~~~~^^^
    # IndexError: list index out of range
    # TODO apply this at relevant place in code, instead
    temp = np.ma.masked_invalid(temp)
  2. moved this from Backlog to Bugs in cf-plot workon May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions