1

Is it possible to change RectangleSelector properties after it has been created?

Let's say I created a figure with the following rectangle:

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

def on_click(eclick, erelease):
    pass

fig, current_ax = plt.subplots()
RS = RectangleSelector(current_ax, on_click, drawtype='box', 
                        useblit=True, button=[1, 3], interactive=True,
                        minspanx=1, minspany=1, spancoords='pixels',
                        rectprops=dict(linestyle='-', color='g', 
                                       fill=True, alpha=1))
plt.show()

What if I want to change the rectangle color?

I would do:

RS.rectprops['color'] = 'r'
RS.canvas.draw()
fig.canvas.draw()

But this doesn't affect the color of the rectangle.

Any clue?

Thanks

2
  • 2
    RS.artists[0].set_facecolor('r') could work Commented Mar 4, 2021 at 13:57
  • Thanks. Both your comment and the accepted answer work fine! Commented Mar 4, 2021 at 14:05

1 Answer 1

1

If you want to update the rectangle patch you have drawn on the figure, not the widget, you can use:

current_ax.patches[0].set_facecolor('r')

or alternatively (credit to JohanC):

RS.artists[0].set_facecolor('r')

After clicking on the rectangle again, it will change color

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.