1

I have three white buttons [A,B,C]. When either of these buttons are clicked on they should turn green instantly. When multiple buttons are clicked on sequentially, only the most recently clicked on should turn green, the others should return to their original white state.

I've read this page Change matplotlib Button color when pressed, but it didn't help. Here is the code that I've written. At the moment the button isn't even turning green. I'm using Matplotlib 2.0.

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

fig, ax = plt.subplots()


button_axcut={}
button={}

button_axcut['A'] = plt.axes([0.25,0.5, 0.1, 0.1])
button['A'] = Button(button_axcut['A'] ,'A', color='white')

button_axcut['B'] = plt.axes([0.45,0.5, 0.1, 0.1])
button['B'] = Button(button_axcut['B'] ,'B', color='white')

button_axcut['C'] = plt.axes([0.65,0.5, 0.1, 0.1])
button['C'] = Button(button_axcut['C'],'C', color='white')


def A_clicked(event):
    print('A')
    button['A'].ax.set_facecolor('green')
    button['B'].ax.set_facecolor('white')
    button['C'].ax.set_facecolor('white')

    button['A'].ax.figure.canvas.draw()
    button['B'].ax.figure.canvas.draw()
    button['C'].ax.figure.canvas.draw()

def B_clicked(event):
    print('B')
    button['A'].ax.set_facecolor('white')
    button['B'].ax.set_facecolor('green')
    button['C'].ax.set_facecolor('white')

    button['A'].ax.figure.canvas.draw()
    button['B'].ax.figure.canvas.draw()
    button['C'].ax.figure.canvas.draw()

def C_clicked(event):
    print('C')
    button['A'].ax.set_facecolor('white')
    button['B'].ax.set_facecolor('white')
    button['C'].ax.set_facecolor('green')

    button['A'].ax.figure.canvas.draw()
    button['B'].ax.figure.canvas.draw()
    button['C'].ax.figure.canvas.draw()

button['A'].on_clicked(A_clicked)
button['B'].on_clicked(B_clicked)
button['C'].on_clicked(C_clicked)

enter image description here

1 Answer 1

0

When I run the code as script, adding plt.show() at the end, it's working fine already. This was tested with matplotlib 2.0 in python 2.7, using the Qt4Agg as well as the TkAgg backend.

One can condense the code a bit and make sure the buttons keep their color, when moving the mouse out of the axes as follows:

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

fig, ax = plt.subplots()


button_axcut={}
button={}

button_axcut['A'] = plt.axes([0.25,0.5, 0.1, 0.1])
button['A'] = Button(button_axcut['A'] ,'A', color='white')

button_axcut['B'] = plt.axes([0.45,0.5, 0.1, 0.1])
button['B'] = Button(button_axcut['B'] ,'B', color='white')

button_axcut['C'] = plt.axes([0.65,0.5, 0.1, 0.1])
button['C'] = Button(button_axcut['C'],'C', color='white')

def click(event):
    for b in ["A", "B", "C"]:
        if button[b].ax == event.inaxes:
            button[b].ax.set_facecolor('green')
            button[b].color = 'green'
        else:
            button[b].ax.set_facecolor('white')
            button[b].color = 'white'
    fig.canvas.draw_idle()


for b in ["A", "B", "C"]:
    button[b].on_clicked(click)

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

2 Comments

i've added a print(b) under the if statement, and it's showing that the buttons are working fine, however the colors are still not changing to green. thanks for condensing my code it reads much better now.
Can you update the question to include a detailed description how you run the code? Also, on which system? Can you try to use a different backend?

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.