5

How do you change the label of a matplotlib button on clicking it? Like if I have a matplotlib button with the label "Before", I want to click it and change the label to "After". How do I do this? I feel that I should know this but I really can't find anything on this online or in the documentation.

2
  • 3
    You capture the click event, then programatically change the label. Event handling here: matplotlib.org/1.3.1/users/event_handling.html Commented May 16, 2014 at 23:11
  • 2
    How would you "programmatically change the label"? Commented May 16, 2014 at 23:28

1 Answer 1

10

assuming this example http://matplotlib.org/1.3.1/examples/widgets/buttons.html

bprev.label="A new Label!" #actually this doesnt work...

I figured this out with next to no knowledge of buttons simply by looking at the docs (really just glancing at them) http://matplotlib.org/1.3.1/api/widgets_api.html#Button

a little more experimentation led me to

bprive.label.set_text("A new label") # works
Sign up to request clarification or add additional context in comments.

4 Comments

you probably have to have interactive mode on ion() I think ... or it would not actually update the image
didn't work either. Maybe I'll try something else. Thanks for the answers!
@lkloh see edit :P apparently i was wrong initially
I needed to call plt.draw() after for the change to take effect.

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.