Skip to content
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

Remove matplotlib 3.8+ pin #160

Closed
kafitzgerald opened this issue Sep 22, 2023 · 2 comments · Fixed by #177
Closed

Remove matplotlib 3.8+ pin #160

kafitzgerald opened this issue Sep 22, 2023 · 2 comments · Fixed by #177
Assignees
Labels
bug Something isn't working

Comments

@kafitzgerald
Copy link
Collaborator

kafitzgerald commented Sep 22, 2023

Describe the bug
The plot_contour_labels usage example is not working properly with matplotlib 3.8+.

Most of this was because of issues with matplotlib and cartopy many of which have now been addressed, but few label locations are impacted by matplotlib/matplotlib#27333. Particularly for this usage example I think we should still just remove this pin and thin the number of labels (including the problematic ones).

You can see the problems here:

IndexError                                Traceback (most recent call last)
Cell In[12], line 41
     35 manual = [(-70, 55000), (-80, 26000), (-72, 22500), (-62, 40000), (-58, 30000),
     36           (-45, 69500), (-40, 34000), (-12, 39000), (-37, 75000), (40, 50000),
     37           (-25, 42000), (18, 23000), (30, 40000), (45, 40000), (57, 41000),
     38           (63, 39000), (55, 80000), (65, 85000)]
     40 manual = [(-70, 55000), (-80, 26000), (55, 80000), (65, 85000)]
---> 41 clabels = ax1.clabel(contours,
     42                      levels=np.arange(0, 25, 8),
     43                      fontsize=12,
     44                      colors="black",
     45                      fmt="%.0f",
     46                      manual=manual)
     47 plt.show()

File ~/miniconda3/envs/geocat/lib/python3.11/site-packages/matplotlib/axes/_axes.py:6550, in Axes.clabel(self, CS, levels, **kwargs)
   6532 def clabel(self, CS, levels=None, **kwargs):
   6533     """
   6534     Label a contour plot.
   6535 
   (...)
   6548         All other parameters are documented in `~.ContourLabeler.clabel`.
   6549     """
-> 6550     return CS.clabel(levels, **kwargs)

File ~/miniconda3/envs/geocat/lib/python3.11/site-packages/matplotlib/contour.py:210, in ContourLabeler.clabel(self, levels, fontsize, inline, inline_spacing, fmt, colors, use_clabeltext, manual, rightside_up, zorder)
    208 if np.iterable(manual):
    209     for x, y in manual:
--> 210         self.add_label_near(x, y, inline, inline_spacing)
    211 elif manual:
    212     print('Select label locations manually using first mouse button.')

File ~/miniconda3/envs/geocat/lib/python3.11/site-packages/matplotlib/contour.py:590, in ContourLabeler.add_label_near(self, x, y, inline, inline_spacing, transform)
    587 label_width = self._get_nth_label_width(level)
    588 rotation, path = self._split_path_and_get_label_rotation(
    589     path, idx_vtx_min, proj, label_width, inline_spacing)
--> 590 self.add_label(*proj, rotation, self.labelLevelList[idx_level_min],
    591                self.labelCValueList[idx_level_min])
    593 if inline:
    594     self._paths[idx_level_min] = path

IndexError: list index out of range

Likely related to these changes:

Related issues over on cartopy:

Where I'm at in sorting this out:

  • Have tested with the latest matplotlib and cartopy and am seeing the problems (fine w/ latest matplotlib 3.7)
  • Messed around a bit with the plot_contour_labels example and it seems only some label locations trigger the issue (generally those closest to the edge of the contour / projected space)
  • Similarly the NCL_coneff_8.py example on fails for certain labels
  • Unfortunately sorting this out will likely require a deeper dive in matplotlib / cartopy than I have time for at the moment

I think the should throw in a version pin for the time being.

Side note:

  • We could also check out what cartopy is doing for testing.
@kafitzgerald
Copy link
Collaborator Author

Actually, I think this may be a bug in matplotlib. Something doesn't seem to be working properly in the code that finds the nearby contours to label.

Unfortunately we don't get an error in the plot_contour_labels usage example like we do in geocat-examples, but the usage example is also impacted. See here: https://geocat-viz.readthedocs.io/en/latest/examples/plot_contour_labels.html

I think we should probably pin to <3.8 and I'll report something over on the matplotlib repo once I have a little better of an idea what's going on.

@kafitzgerald kafitzgerald changed the title plot_contour_labels is broken by API change in matplotlib 3.8 plot_contour_labels is broken by matplotlib 3.8+ Sep 29, 2023
@kafitzgerald kafitzgerald changed the title plot_contour_labels is broken by matplotlib 3.8+ plot_contour_labels is broken by matplotlib 3.8 Sep 29, 2023
@kafitzgerald kafitzgerald changed the title plot_contour_labels is broken by matplotlib 3.8 Support for matplotlib 3.8+ Oct 2, 2023
@kafitzgerald
Copy link
Collaborator Author

kafitzgerald commented Nov 15, 2023

Ok, it looks like this is more than one issue though some have been resolved by fixes upstream.

The remaining (after updating matplotlib and cartopy) spurious lines in the plot_contour_labels usage example are occurring because of one remaining bug in matplotlib. In particular, these three points are problematic:

(-58.78, 67.05)
(-102.69, 73.62)
(-78.02, 52.22)

The remaining labels still overlap a bit more than they used to, but I think we should probably just thin out the number of labeled contour locations for the usage example.

@kafitzgerald kafitzgerald changed the title Support for matplotlib 3.8+ Remove matplotlib 3.8+ pin Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants