我正在使用 matplotlib 的 FuncAnimation
运行动画来显示来自微处理器的数据(实时)。我正在使用按钮向处理器发送命令,并希望按钮的颜色在单击后发生变化,但我在 matplotlib.widgets.button
文档中找不到任何内容(目前)实现了这一点。
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()
关于python - 按下时更改 matplotlib 按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17707169/