Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Matplotlib和checkbuttons自定义_Python_Matplotlib - Fatal编程技术网

Python Matplotlib和checkbuttons自定义

Python Matplotlib和checkbuttons自定义,python,matplotlib,Python,Matplotlib,我正在使用matplotlib中的绘图,其中多条线由一个条目表示。特别是在拾取单个图例条目时取消选择或选择多行;为了清晰起见,我从matplotlib文档(和)中的演示开始,并对其进行了一些修改: %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 =

我正在使用matplotlib中的绘图,其中多条线由一个条目表示。特别是在拾取单个图例条目时取消选择或选择多行;为了清晰起见,我从matplotlib文档(和)中的演示开始,并对其进行了一些修改:

%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()
输出如下:

我想解决两类问题:

  • 我不知道如何循环创建“lx”值(我有很多行要创建,我不想写“l0”、“l1”、“l2”…等等)
  • 我想在checkbutton上有不同的外观。我特别希望在选中时有一个用线颜色填充的矩形,在取消选中时有一个空矩形(白色背景)
  • 我试着在文档中寻找帮助,但我是一个新手,我现在陷入了困境。 谁能帮帮我吗

    谢谢

    试试这个

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

    多亏了艾琳的贡献和重要性,我解决了第一个问题。对于第二个问题,我找到了一种解决方法,在复选框中输入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()
    

    我认为问题在于问题中4Hz有两条线路,但2Hz只有一条。第一个问题现在已经回答了,谢谢!第二,我认为有问题,因为尝试打开/关闭不同的情节会导致“有趣”的行为。