BUG: Fix UnboundLocalError in contour labelling by dopplershift · Pull Request #10710 · matplotlib/matplotlib · GitHub
Skip to content

BUG: Fix UnboundLocalError in contour labelling - #10710

Merged
jklymak merged 1 commit into
matplotlib:masterfrom
dopplershift:fix-error
Mar 7, 2018
Merged

jklymak merged 1 commit into
matplotlib:masterfrom
dopplershift:fix-error

Conversation

@dopplershift

@dopplershift dopplershift commented Mar 7, 2018

Copy link
Copy Markdown
Contributor

This fixes hitting the following traceback with 2.2.0 (problem also exists on master):

  File "/home/travis/miniconda/envs/gallery/lib/python3.5/site-packages/matplotlib/pyplot.py", line 2790, in clabel
    ret = ax.clabel(CS, *args, **kwargs)
  File "/home/travis/miniconda/envs/gallery/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 6173, in clabel
    return CS.clabel(*args, **kwargs)
  File "/home/travis/miniconda/envs/gallery/lib/python3.5/site-packages/matplotlib/contour.py", line 213, in clabel
    self.labels(inline, inline_spacing)
  File "/home/travis/miniconda/envs/gallery/lib/python3.5/site-packages/matplotlib/contour.py", line 646, in labels
    inline_spacing)
  File "/home/travis/miniconda/envs/gallery/lib/python3.5/site-packages/matplotlib/contour.py", line 445, in calc_label_rot_and_inline
    nlc.append(np.row_stack([xy2, lc[I[1]:I[0]+1], xy1]))
UnboundLocalError: local variable 'xy2' referenced before assignment

That traces back to this block:

The code was trying to protect the access of xy1 and xy2 to valid cases, using np.all(). Unfortunately, in this case I is a list, so I != -1 doesn't do element-by-element or
return an error--it just always returns True. That caused the if to not actually guard anything. Replaced with all and a generator expression.

I tried making a test, but I couldn't find any sensible set of data to reproduce. We hit this in an example of ours that was using cartopy incorrectly (fixing that problem makes the error disappear). It appears to only trigger when you have a really tiny contour (in display coords) that can't be properly interpolated to create a spot for the contour label.

cc @anntzer since you introduced this code according to git.

The code was trying to protect the access of xy1 and xy2 to valid cases,
but unfortunately, for a list, l != -1 doesn't do element-by-element or
return an error--it just always returns True. Therefore it wasn't
actually guarding.
@dopplershift dopplershift added this to the v2.2.1 milestone Mar 7, 2018

@anntzer anntzer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that...

@anntzer anntzer added the Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions. label Mar 7, 2018
@jklymak
jklymak merged commit ef5d01a into matplotlib:master Mar 7, 2018
@dopplershift

Copy link
Copy Markdown
Contributor Author

@dopplershift
dopplershift deleted the fix-error branch March 7, 2018 21:39
jklymak added a commit that referenced this pull request Mar 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Release critical For bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants