为图形循环选择的项目

时间:2019-06-14 20:20:26

标签: python python-3.x anaconda jupyter-lab ipywidgets

我想创建一个@interact函数,以便在复选框中仅选择项目的图形,但无法弄清楚如何在循环中编写它

我编写的代码可以正常工作,但是如果我更改变量则无法扩展


col_list = list(df.columns.values)[0:][1:]
print(col_list)
lista = []

@interact(a=True, b=True, c=True, d=True, e=True, f=True,  g=True, h=True, i=True)
def g(a,b,c,d,e,f,g,h,i):

    if col_list[0] not in lista and a == True:
        lista.append(col_list[0])

    elif col_list[0] in lista and a == False:
        lista.remove(col_list[0])

    elif col_list[1] not in lista and b == True:
        lista.append(col_list[1])

    elif col_list[1] in lista and b == False:
        lista.remove(col_list[1])

    elif col_list[2] not in lista and c == True:
        lista.append(col_list[2])

    elif col_list[2] in lista and c == False:
        lista.remove(col_list[2])

    elif col_list[3] not in lista and d == True:
        lista.append(col_list[3])

    elif col_list[3] in lista and d == False:
        lista.remove(col_list[3])

    elif col_list[4] not in lista and e == True:
        lista.append(col_list[3])

    elif col_list[4] in lista and e == False:
        lista.remove(col_list[3])

    @interact
    def sscale_of_graph(Escala_Tiempo={'y','d','q','sm','m','w'}):
        to_graphic = df[lista].resample(Escala_Tiempo).max().plot()

然后我尝试了这种无效的方法:

col_list = list(df.columns.values)[0:][1:]
##print(col_list)
lista = []

number = [0,1,2,3,4,5,6,7,8]
alpha = ['a','b','c','d','e','f','g','h','i']

@interact(a=True, b=True, c=True, d=True, e=True, f=True,  g=True, h=True, i=True)
def g(a,b,c,d,e,f,g,h,i):

    for i,j in zip(number, alpha):          
        if col_list[i] not in lista and j == True:
            lista.append(col_list[i])
        elif col_list[i] in lista and j == False:
            lista.remove(col_list[i])

        return lista

        print(lista)



    @interact
    def sscale_of_graph(Escala_Tiempo={'y','d','q','sm','m','w'}):
        to_graphic = df[lista].resample(Escala_Tiempo).max().plot()

我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我可以通过以下方式解决:

col_list = list(df.columns.values)[1:]
print('col_list',col_list)



@interact(a=True, b=True, c=True, d=True, e=True, f=True,  g=True, h=True, i=True)
def g(a,b,c,d,e,f,g,h,i):

    lista = []

    number = [0,1,2,3,4,5,6,7,8]
    alpha = [a,b,c,d,e,f,g,h,i]

    for i,j in zip(number, alpha):

        if col_list[i:i+1] not in lista and j == True:
            lista.append(col_list[i:i+1])
        elif col_list[i:i+1] in lista and j == False:
            lista.remove(col_list[i:i+1])

    print('lista',lista)

    if lista == []:
        lista.append(col_list)

    flat_lista = []

    for col in lista:
        for y in col:
            flat_lista.append(y)
    ##print(flat_lista)

    @interact
    def sscale_of_graph(Escala_Tiempo={'y','d','q','sm','m','w'}):
        to_graphic = df[flat_lista].resample(Escala_Tiempo).median().ffill().plot(figsize=(25, 15),fontsize =14)