Hover Tools5:30 with Ken Alger
Adding the ability to interact with our charts makes it easier to see what is happening with the data. Hover tools are an easy way to add some extra functionality to our visualizations.
[MUSIC] 0:00 If you recall, when we last looked at our plot, we had our legends separated by 0:04 color in a continental bucket and I've added in an associated legend. 0:09 However, we are still having some issues with our data visualization in terms of 0:14 being able to tell which glyph is associated with which country. 0:18 Further, will the labels on the x-axis give us a general idea of the population, 0:22 it would be helpful to know the exact number associated with a given country. 0:26 With glyphs representing all of our countries and 0:31 several of them plotted close together. 0:34 Adding labels to each glyph would clutter up our visualization. 0:37 Bokeh provides a solution to this with hover tools, which will allow us to hover 0:40 over a given data point and have the associated data shown about that country. 0:45 Let's have a look from where we left off. 0:49 To start working with hover tools, we need to import HoverTool from bokeh.models. 0:51 If you remember back to previous video, 0:56 we had a parameter tools in our figure object. 0:58 We're going to want to add that back in, 1:01 along with the tools we want to include with the new addition of our hover tool. 1:03 Fit to our plot 1:06 Put in tools, we want pan, 1:10 wheel_zoom, box_zoom, 1:15 reset, hover, and save. 1:20 That adds in our hover tool to the other tools we have seen and are used to. 1:25 I think it is probably time as well to add a title to our plot, 1:30 which can be done with the title parameter being passed into our figure object. 1:34 We'll call it Population versus Life Expectancy. 1:39 And we're still showing our plot and have all the same configurations for 1:45 our color mapper and for our legend and when we run our script. 1:51 And hover over our data points. 2:00 We got a nice little pop-out tool tip, giving us some information. 2:01 So, it is slightly helpful, right? 2:04 It shows us the index number, the x and y values of our charted data, or 2:06 population and life expectancy, and the data points on the plot itself. 2:10 Now we could use that index number to go look in our data to determine 2:15 that index 39 for example is China. 2:20 However, I don't know about you, but 2:23 that seems like a lot off effort to have to look up each point in our raw data. 2:24 Let's connect the information from our raw data directly to the tool tip. 2:28 We need to select our hover tools, and adjust it's visual attributes. 2:32 Since it is assigned to our figure object, we can select it as follows. 2:37 Hover=plot.select_one and we'll assign it for HoverTool. 2:44 Let's take a quick look at the documentation for styling tools in Bokeh. 2:52 I've included a link in the teacher's notes to the Bokeh documentation as well. 2:58 If we scroll down about two thirds of the way on the page, 3:01 we will find a section called setting tool visuals. 3:05 And on the hover tool section there, 3:09 it shows us that we want to pass in a list of couples into our tool tip. 3:11 Now we can access our data items is in the @ sign special character. 3:15 I think for this particular plot, 3:20 adding in the country name in English as well as the values of our data points, 3:22 populations and life expectancy, would be a good start for our tool tip. 3:26 We want to assign to hover.tooltips. 3:31 Country name in English, which is the label for that. 3:44 And assign their data point. 3:54 We'll do this thing for Population. 3:59 And Life Expectancy, in years. 4:11 Now when we run our script and hover over our data point, 4:24 we get the country name, population, and life expectancy in our tool tip. 4:28 You'll notice that if we hover over some of the densely charted data, 4:34 we will get multiple tool tips covering a range of countries that are pretty close 4:37 to where the mouse is pointing. 4:41 This is a great opportunity to use some of the zoom tools, To zoom in on 4:42 an area of our plot and to be able to get a closer look at individual country data. 4:49 When we're done, we can click on the reset tool to reset our display. 4:58 Nice work. 5:04 I think just looking great and is much easier to gather information from and 5:05 about our data set versus looking at rows and rows of data. 5:10 We can take up visualization a bit further though and 5:14 examine other aspects of our data as well. 5:17 Let's have a short break, stand up move around and stretch a bit and 5:20 come back to see some options Bokeh provides us for 5:23 charting multiple data points from the same data source at the same time. 5:26
You need to sign up for Treehouse in order to download course files.Sign up