按下时更改matplotlib按钮的颜色 [英] Change matplotlib Button color when pressed

查看:206
本文介绍了按下时更改matplotlib按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用matplotlib的 FuncAnimation 来运行动画,以实时显示来自微处理器的数据。我正在使用按钮向处理器发送命令,并且希望在单击按钮后改变按钮的颜色,但在 matplotlib.widgets.button 实现此目的的文档(尚未)。

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


推荐答案

只需设置 button.color

例如

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()

这篇关于按下时更改matplotlib按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆