自定义Matplotlib中的检查按钮

时间:2017-02-23 16:33:52

标签: python matplotlib

我正在尝试自定义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))

但无法找到设置按钮框,标记和字体颜色的不透明度(alpha参数)的方法。

任何帮助都是apreciated

1 个答案:

答案 0 :(得分:0)

documentation of matplotlib.widgets.CheckButtons所示,可以从类实例访问标签,按钮矩形和(标记的)行。
使用check = CheckButtons(..)

  • check.rectangles会返回按钮列表'背景为matplotlib.patches.Rectangle
  • check.labels会将标签列表返回为matplotlib.text.Text
  • check.lines会返回两个matplotlib.lines.Line2D的元组列表,作为标记。

所有这些方法都有set_alpha

设置背景最简单的方法是提供一个已经设置了alpha值的颜色,例如col = (0,0,1,0.2),其中最后一个值是蓝色的alpha。可以使用facecolor参数将其设置为检查按钮轴。

这是一个完整的例子。

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

enter image description here