Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib check_buttons box colors

Tags:

matplotlib

I am using the check_buttons widget like in the example (http://matplotlib.org/examples/widgets/check_buttons.html)

On a plot with many lines, it is difficult to know from the text which check box goes with which plotted line, at least until the box is clicked and the user looks to see if he can tell which line has disappeared.

Does anyone know if the background color of the check box could be made the same as the line it affects...something like a legend?

like image 718
sylvanix Avatar asked Jun 05 '26 16:06

sylvanix


1 Answers

You can set the widget box colour as follows,

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons

t = np.arange(0.0, 2.0, 0.01)
s0 = np.sin(2*np.pi*t)
s1 = np.sin(4*np.pi*t)
s2 = np.sin(6*np.pi*t)

fig, ax = plt.subplots()
l0, = ax.plot(t, s0, visible=False, lw=2)
l1, = ax.plot(t, s1, lw=2)
l2, = ax.plot(t, s2, lw=2)
plt.subplots_adjust(left=0.2)

rax = plt.axes([0.05, 0.4, 0.1, 0.15])
check = CheckButtons(rax, ('2 Hz', '4 Hz', '6 Hz'), (False, True, True))

#Define colours for rectangles and set them
c = ['b', 'g', 'r']    
[rec.set_facecolor(c[i]) for i, rec in enumerate(check.rectangles)]


def func(label):
    if label == '2 Hz': l0.set_visible(not l0.get_visible())
    elif label == '4 Hz': l1.set_visible(not l1.get_visible())
    elif label == '6 Hz': l2.set_visible(not l2.get_visible())
    plt.draw()
check.on_clicked(func)

plt.show()

The checkbutton panel has each tick box as a matplotlib.patches.Rectangle object which can be customised as needed.

like image 174
Ed Smith Avatar answered Jun 09 '26 00:06

Ed Smith