Search code examples
pythondictionarytooltipipywidgetsbqplot

Using bqplot tooltip with a dataframe


Use widget to display snippets from a csv file/dataframe and feed to tooltip attribute of bqplot

I am trying to display certain info (statistics such as age group, income, obesity levels etc.) for each state in the US using bqplot. I am able to plot the US map using examples provided on bqplot github. But the tooltip constructor is using columns names from a json file, which is also used to actually display the map. I do not know how to override that so, I decided to use a widget (ideally a container widget) to hold the data I want to display for each state and use the on_hover() method provided in bqplot.map; In fact, I just modified the code in the link below:

Container as tooltip doesn't show contents

to display info for a particular state


import bqplot
import pandas as pd
import ipywidgets as ipw
from IPython.display import display, clear_output

res = pd.read_csv('data_files/clean_data.csv')
df = pd.DataFrame(res)

## using values for only one state
df_temp = df[df['State'] == 'NY']

out = ipw.Output()
def hover_handler(self,Dataframe):
        out.clear_output()
        with out:

            display(ipw.HTML(df_temp.to_html()))


geo = AlbersUSA()
scale_s =  OrdinalColorScale(domain=['Obese', 'Not Obese'], colors=['Red', 'Green'])
st_green = dict(zip(df['State'], list(obesity_list)))

geo = AlbersUSA()
scale_s =  OrdinalColorScale(domain=['Obese', 'Not Obese'], colors=['Red', 'Green'])
map_args = {'color': st_green,
              'scales': {'projection': geo, 'color': scale_s}, 
              'colors': {'default_color': 'Black'}}   

s_map = plt.geo(map_data='USStatesMap', **map_args)
s_map.toolip = out  # I am pointing tooltip to the out object
s_map.on_hover(hover_handler)
s_map
  1. I cannot figure how to change the color in each state based on values 'Obese' and 'Not obese';

  2. I used snippets of code from bqplot example and the link above on stackoverflow but I do not know how to make the info change as I hover over to another state; Currently df_temp holds values for only one state and that is displayed anywhere I hover over the map. How can I make the widget fetch relevant data?

2.1 Essentially, I need it to display results in a tabular format:

State:NY
Status: Not Obese
Age_group | Obese | Not_obese
<18       |  %    |  %
19-30     |  %    |  %

I have data for all the fields above as columns in a dataframe for EACH state. I am new to ipywidgets and bqplot. So, any help would be greatly appreciated.

Thank you!


Solution

  • For Point 2:

    In your hover_handler code, you need to capture the information being passed through to the function, and use it to filter your master dataframe (df). Try changing your hover_handler code to the below, and see what info is being passed to the function. Then work out how to use this information to filter your dataframe to just the relevant state.

        def hover_handler(map, hover_event):
                out.clear_output()
                with out:
                    print(hover_event)