1

I'm busy with programming an interactive graph where the values in the graph are chosen based on columnvalues from a dataframe. So, I want to make a graph of values in column x where only the values are chosen which fullfil several conditions from another columns in the dataframe. I have the following dataframe (data): Dataframe

data = pd.DataFrame([[3,'VW-b','1 R- R',119.080,'TS',13.56], 
                 [3,'VW-b','1 R- R',119.090,'RS',18.56], 
                 [3,'VW-b','1 R- R',119.100,'TS',12.85],
                 [3,'VW-b','1 R- R',119.110,'TS',9.56],
                 [3,'VW-b','1 R- R',119.120,'RS',8.86],
                 [3,'VW-b','1 R- R',119.130,'TS',13.56],
                 [3,'VW-b','1 R- R',119.140,'TS',19.56],
                 [3,'VW-b','1 R- R',119.150,'TS',14.56],
                 [3,'HRL','1 R- R',159.080,'RS',13.56],
                 [3,'VW-c','1 R- R',19.080,'RS',13.56],
                 [3,'VW-s','1 R- R',254.080,'TS',13.56],
                 [3,'VW-b','1 R- R',25.020,'TS',13.56],
                 [3,'VW-b','1 R- R',25.030,'TS',16.56]], 
                columns=['IGO', 'Baan','Strook','Km','Spoor','IDK1-2'])

My imports are:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import ipywidgets as widgets
from ipywidgets import interact
from ipywidgets import interactive
from IPython.display import display

%matplotlib inline

I wrote the following code:

def choice(igo, baan, strook, spoor, kmv, kmt):
    Igo = data['IGO'] == igo
    Baan = data['Baan'] == baan
    Strook = data['Strook'] == strook
    Spoor = data['Spoor'] == spoor
    Kmv = data['Km'] == kmv
    Kmt = data['Km'] == kmt


    plt.plot(data['IDK1-2'])
    plt.xlim(kmv,kmt)

w = interactive(choice,
     igo=widgets.Dropdown(options=data['IGO'].unique(),description='IGO'),
     baan=widgets.Dropdown(options=data['Baan'].loc[data['IGO'] == igo].unique(),description='Baan'),
     strook=widgets.Dropdown(options=data['Strook'].loc[data['Baan'] == baan].unique(),description='Strook'),
     spoor=widgets.RadioButtons(options=data['Spoor'].unique(),value='TS',description='Spoor'),
     kmv=widgets.Dropdown(options=data['Km'].unique(),description='Van km'),
     kmt=widgets.Dropdown(options=data['Km'].unique(),description='Tot km'))
display(w)

How can I make the code in such a way that I can choose first the IGO, than the baan (available choices based on IGO) etc?

NWI
  • 11
  • 2
  • Please include all your imports, and create an example dataframe from a dictionary so we can copy, paste and run your code (see here: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples for how to make a dataframe from a dict). – ac24 Apr 13 '21 at 08:57
  • I don't think you can use `interactive` if you also want your input widgets to change as part of the interactive function call. This will create an infinite loop, where widget change -> function call -> widget change -> function call ... etc. I think there is a solution with `observe` instead. – ac24 Apr 13 '21 at 09:41
  • OK, clear. But how can I use the `observe` solution? – NWI Apr 13 '21 at 09:44
  • I mean, how do I use it with more than one column to check? – NWI Apr 13 '21 at 10:59
  • Thinking about this some more, I don't think it is easily achievable in the ipywidgets framework. You need a widget that can have its value set without triggering a function run, but when you change the value manually, the function run does trigger. You need this to avoid an infinite loop. I don't think ipywidgets can offer that level of control. You could try looking at qgrid as a dataframe viewer in notebook. – ac24 Apr 14 '21 at 10:36

0 Answers0