Skip to content

examples/widgets/span_selector.py is brittle #20200

Closed

Description

@anntzer

Problem

examples/widgets/span_selector.py crashes if one clicks somewhere and immediately releases the click, without dragging the SpanSelector, with

Traceback (most recent call last):
  File ".../matplotlib/lib/matplotlib/cbook/__init__.py", line 274, in process
    func(*args, **kwargs)
  File ".../matplotlib/lib/matplotlib/widgets.py", line 1840, in release
    self._release(event)
  File ".../matplotlib/lib/matplotlib/widgets.py", line 2069, in _release
    self.onselect(vmin, vmax)
  File ".../matplotlib/examples/widgets/span_selector.py", line 36, in onselect
    ax2.set_xlim(region_x[0], region_x[-1])
IndexError: index 0 is out of bounds for axis 0 with size 0

Suggested Improvement

Make the example more robust.

Matplotlib version

  • Operating system: linux
  • Matplotlib version (import matplotlib; print(matplotlib.__version__)): 3.4+HEAD
  • Matplotlib documentation version (is listed under the logo): 3.4.2.post693

Activity

  1. Jashshah6086 commented on May 12, 2021

    @Jashshah6086
    Contributor

    I would like to work on it.

  2. anntzer commented on May 12, 2021

    @anntzer
    ContributorAuthor

    It's yours.

  3. Jashshah6086 commented on May 12, 2021

    @Jashshah6086
    Contributor

    #20212 (comment)

    • Why Error occured?
      when pointer is not dragged list is either null or has null element.Which created error in indexing.

    • Solution:
      Added condition on length of region_x

    if len(region_x) >= 2:
            line2.set_data(region_x, region_y)
            ax2.set_xlim(region_x[0], region_x[-1])
            ax2.set_ylim(region_y.min(), region_y.max())
            fig.canvas.draw()
    

    instead of

    line2.set_data(region_x, region_y)
    ax2.set_xlim(region_x[0], region_x[-1])
    ax2.set_ylim(region_y.min(), region_y.max())
    fig.canvas.draw()
    
  4. QuLogic commented on May 17, 2021

    @QuLogic
    Member

    Closed by #20212.

  5. added this to the v3.4.3 milestone on May 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Documentation🌱 Good first issueOpen a pull request against these issues if there are no active ones!

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions