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

BUG: RectangleSelector modifies view #18477

Open
larsoner opened this issue Sep 14, 2020 · 4 comments
Open

BUG: RectangleSelector modifies view #18477

larsoner opened this issue Sep 14, 2020 · 4 comments

Comments

@larsoner
Copy link
Contributor

larsoner commented Sep 14, 2020

Bug report

Bug summary

RectangleSelector:

  1. Modifies dataLim
  2. This modification seemingly cannot be reverted by removing the patch, or by modifying the patch properties

Basically I want to be able to use RectangleSelector without it modifying ax.dataLim, because if it does it messes up autoscale.

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.widgets import RectangleSelector

fig, ax = plt.subplots()
lim = ax.dataLim
ax.plot([-0.1, 0.1], [-0.1, 0.1])
assert lim.x0 == lim.y0 == -0.1
assert lim.width == lim.height == 0.2
for kind in ('rect', 'rs'):
    if kind == 'rect':
        rect = Rectangle((0, 0), 0, 1)
        ax.add_patch(rect)
    else:
        rs = RectangleSelector(ax, lambda x: None)
        rect = rs.to_draw
    assert isinstance(rect, Rectangle)
    ax.relim()
    assert lim.x0 == lim.y0 == -0.1
    assert lim.width == 0.2
    assert lim.height == 1.1
    ax.patches.remove(rect)
    assert ax.patches == []
    ax.relim()
    assert lim.height == 0.2, (kind, lim.height)

The plain "Rectangle" version passes, but the "RectangleSelector" version does not:

    assert lim.height == 0.2, (kind, lim.height)
AssertionError: ('rs', 1.1)

And the resulting plot is "stuck" with a minimum autolim y max of 1. I also tried setting properties of the Rectangle patch that's part of the RectangleSelector (xy, width, height) and calling ax.relim() but the bound of 1. never decreases.

Matplotlib version

  • Operating system: Linux x86_64
  • Matplotlib version: latest master
  • Matplotlib backend: Qt5Agg
  • Python version: 3.8
@QuLogic
Copy link
Member

QuLogic commented Sep 15, 2020

ax.patches.remove(rect)

Not going to fix anything, but you should use rect.remove() instead of touching ax.patches.

@PrateekArya
Copy link

PrateekArya commented Feb 22, 2021

I am facing the same issue. This seems to be same as #19067.

@dstansby
Copy link
Member

I'm a bit confused as to what's going on here - is the desired behaviour that adding a RectangleSelector to an Axes doesn't modify the Axes limits?

@larsoner
Copy link
Contributor Author

It has been a while since I thought about this, but yes I think so. Rectangle behaves this way but RectangleSelector does not, it seems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants