Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: Fix UnboundLocalError in contour labelling #10710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2018

Conversation

dopplershift
Copy link
Contributor

@dopplershift dopplershift commented Mar 7, 2018

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:

I = np.interp(xi, pl, np.arange(len(pl)), left=-1, right=-1)
I = [np.floor(I[0]).astype(int), np.ceil(I[1]).astype(int)]
if I[0] != -1:
xy1 = [np.interp(xi[0], pl, lc_col) for lc_col in lc.T]
if I[1] != -1:
xy2 = [np.interp(xi[1], pl, lc_col) for lc_col in lc.T]
# Actually break contours
if closed:
# This will remove contour if shorter than label
if np.all(I != -1):
nlc.append(np.row_stack([xy2, lc[I[1]:I[0]+1], xy1]))

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
Copy link
Contributor

@anntzer anntzer left a comment

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
Contributor Author

No worries @anntzer ! Making our own code actually work correctly was a suitable workaround. 😁 If it weren't for this bug, who knows when we would have realized...

@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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.