Matplotlib和自定义按钮

时间:2018-12-13 11:19:44

标签: python matplotlib

我正在matplotlib中处理一个绘图,其中多条线由一个条目表示。特别是在选择单个图例条目时取消选择或选择多行;为清楚起见,我从matplotlib文档(https://matplotlib.org/gallery/widgets/check_buttons.htmlhttps://matplotlib.org/api/widgets_api.html#matplotlib.widgets.CheckButtons)中的演示开始,我做了一些修改:

%matplotlib notebook
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)
s3 = 2*np.sin(4*np.pi*t)

fig, ax = plt.subplots()
l0, = ax.plot(t, s0, lw=2,c='r')
l1, = ax.plot(t, s1, lw=2,c='b')
l2, = ax.plot(t, s2, lw=2,c='g')
l3, = ax.plot(t, s3, lw=2,c='b')
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'), (True, True, True))

#Define colours for rectangles and set them
c = ['r', 'b', 'g']    
[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())
        l3.set_visible(not l3.get_visible())
    elif label == '6 Hz':
        l2.set_visible(not l2.get_visible())
    plt.draw()
check.on_clicked(func)

plt.show()

输出为:

matplotlib_checkbuttons_example

我想解决两种问题:

  1. 我不知道如何循环创建“ lx”值(我有很多行要创建,我不想写“ l0”,“ l1”,“ l2”等)
  2. 我希望复选按钮出现不同的外观。特别是,我希望选择一个填充有线条颜色的矩形,而取消选择一个空白的矩形(白色背景)。

我尝试在文档中寻求帮助,但我是新手,因此目前处于困境。 有人可以帮我吗?

谢谢

2 个答案:

答案 0 :(得分:2)

由于AILearning和ImportanceOfBeingErnest的贡献,我解决了第一个问题。对于第二个问题,我找到了一种解决方法,将0线宽放在复选框中,并仅在可见或不可见时才激活或禁用背景色。最终答案是:

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)
s3 = 2*np.sin(4*np.pi*t)

fig, ax = plt.subplots()

l=[]
for si,c,label in zip([s0,s1,s2,s3],['r','b','g','b'],('2 Hz', '4 Hz', '6 Hz','4 Hz')):
    l.append(ax.plot(t, si, lw=2,c=c,label=label)[0])


plt.subplots_adjust(left=0.2)
labels = [str(line.get_label()) for line in l]



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

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

def func(label):
    id_lab = [i for i, s in enumerate(check.labels) if label in s._text]
    for index,lab in enumerate(labels):
        if lab==label:
            l[index].set_visible(not l[index].get_visible())
            if (l[index].get_visible()):
                check.rectangles[id_lab[0]].set_fill(True)
            else:
                check.rectangles[id_lab[0]].set_fill(False)
    plt.draw()

check.on_clicked(func)

plt.show()

答案 1 :(得分:1)

试试看!

%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import CheckButtons[![enter image description here][1]][1]
from matplotlib.patches import Rectangle

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)
s3 = 2*np.sin(4*np.pi*t)

fig, ax = plt.subplots()

l=[]
for si,c,label in zip([s0,s1,s2,s3],['r','b','g','b'],('2 Hz', '4 Hz', '6 Hz','4 Hz')):
    l.append(ax.plot(t, si, lw=2,c=c,label=label)[0])


plt.subplots_adjust(left=0.2)
labels = [str(line.get_label()) for line in l]



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

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



def func(label):
    for index,lab in enumerate(labels):
        if lab==label:
            l[index].set_visible(not l[index].get_visible())
            check.rectangles[index].set_fill(True)
        else:
            check.rectangles[index].set_fill(False)
    plt.draw()

check.on_clicked(func)

plt.show()

enter image description here