2

I'm running an animation using matplotlib's FuncAnimation to display data (live) from a microprocessor. I'm using buttons to send commands to the processor and would like the color of the button to change after being clicked, but I can't find anything in the matplotlib.widgets.button documentation (yet) that achieves this.

class Command:

    def motor(self, event):
    SERIAL['Serial'].write(' ')
    plt.draw()

write = Command()
bmotor = Button(axmotor, 'Motor', color = '0.85', hovercolor = 'g')
bmotor.on_clicked(write.motor)            #Change Button Color Here

3 Answers 3

5

Just set button.color.

E.g.

import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import itertools


fig, ax = plt.subplots()
button = Button(ax, 'Click me!')

colors = itertools.cycle(['red', 'green', 'blue'])

def change_color(event):
    button.color = next(colors)
    # If you want the button's color to change as soon as it's clicked, you'll
    # need to set the hovercolor, as well, as the mouse is still over it
    button.hovercolor = button.color
    fig.canvas.draw()

button.on_clicked(change_color)

plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

In this example there is a single button. But what if there are several buttons, and you would like to change the color of the one clicked?
@KurtPeek - First off, each button is connected to its own callback, so you'll typically connect different callback functions to different buttons when you want different behavior. (E.g. the case above uses a closure, so could use a more explicit lambda or functools.partial, etc instead). However, if you need to, you can compare event.inaxes to button.ax. Each Button instance takes up an entire Axes, so the axes that the event occurs in will correspond to the button. There are some caveats there if you have overlapping axes, however (e.g. a button inside another axes).
3

In current matplotlib version (1.4.2) 'color' and 'hovercolor' are took into account only when mouse '_motion' event has happened, so the button change color not when you press mouse button, but only when you move mouse afterwards.

Nevertheless, you can change button background manually:

import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import itertools

button = Button(plt.axes([0.45, 0.45, 0.2, 0.08]), 'Blink!')


def button_click(event):
    button.ax.set_axis_bgcolor('teal')
    button.ax.figure.canvas.draw()

    # Also you can add timeout to restore previous background:
    plt.pause(0.2)
    button.ax.set_axis_bgcolor(button.color)
    button.ax.figure.canvas.draw()


button.on_clicked(button_click)

plt.show()

1 Comment

use set_facecolor instead of set_axis_bgcolor for current matplotlib versions
0

In case anyone do not want to make the Button as a global variable when changing the colour, here is a solution:

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

def change_color(button):
    button.color = 'red'

def main():
    button = Button(plt.axes([0.5, 0.5, 0.25, 0.05]), 'Click here')
    button.on_clicked(lambda _: change_color(button))
    plt.show()

if __name__ == "__main__":
    main()

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.