I am trying to customize the CheckButtons:
rax = plt.axes(
[0.55, 0.8, 0.08, 0.08],
facecolor=secondary_color
)
check = CheckButtons(rax, ('Mn vs. Pn', 'ØMn vs. ØPn'), (True, True))
but can't find a way to set the opacity (alpha parameter) of the button box, the marker and the font color.
Any help would be apreciated
As seen in the documentation of matplotlib.widgets.CheckButtons, the labels, the button rectangles and the lines (of the marker) can be accessed from the class instance.
With check = CheckButtons(..)
check.rectangles returns a list of the buttons' backgrounds as matplotlib.patches.Rectanglecheck.labels returns a list of the labels as matplotlib.text.Textcheck.lines returns a list of tuples of two matplotlib.lines.Line2D which serve as the markers.All of them have set_alpha methods.
To set the background the easiest way is to provide a color with an alpha value already being set, like col = (0,0,1,0.2) where the last value is the alpha of the blue color. This can be set to the checkbutton axes using the facecolorargument.
Here is a complete example.
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons
fig= plt.figure(figsize=(4,1.5))
ax = plt.axes([0.4, 0.2, 0.4, 0.6] )
ax.plot([2,3,1])
col = (0,0.3,0.75,0.2)
rax = plt.axes([0.1, 0.2, 0.2, 0.6], facecolor=col )
check = CheckButtons(rax, ('red', 'blue', 'green'), (1,0,1))
for r in check.rectangles:
r.set_facecolor("blue")
r.set_edgecolor("k")
r.set_alpha(0.2)
[ll.set_color("white") for l in check.lines for ll in l]
[ll.set_linewidth(3) for l in check.lines for ll in l]
for i, c in enumerate(["r", "b", "g"]):
check.labels[i].set_color(c)
check.labels[i].set_alpha(0.7)
plt.show()

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With