Python Tutorial: Customizing glyphs
Skills:
Python for Data80%
Key Takeaways
This video tutorial demonstrates how to customize glyphs in Bokeh, a Python library for interactive data visualization, by altering their visual appearance in response to user interactions such as selection and hover, and by using color mappers to shade groups of data differently.
Full Transcript
we've seen how to create and use glyphs and specify their basic visual properties sometimes it's useful to be able to alter the appearance of a glyph in certain situations for instance when a user makes a selection or hovers over the glyphs let's see how the visual appearance of glyphs can be altered in these situations boquete has several different tools to enable users to select points on a plot using a mouse or touch interactions in this example we create a figure that specifies tools equals box select comma lasso select this configures our plot to have two different kinds of selection tools a box select tool that allows users to select points by dragging a rectangular region over the plot and a lasso tool that allows points to be selected by drawing a free-form curve when points are selected using one of bouquets selection tools how is this fact made visually evident by default once the selection is made bouquet draws all the non selected points with a very low transparency or alpha value however it's possible to customize this behavior to do so we simply pass additional arguments to the glyph method these arguments are the same visual properties we've already seen such as fill color except that they either have the prefix selection or non selection let's take a look at the call to circle in this example here we specify selection color equals red this configures the plot so that whenever a selection is active all the selected points have a fill color of red note that the argument color is just a shorthand for specifying fill color and line color at the same time additionally we specify non selection fill alpha equals 0.2 and non selection fill color equals gray these two arguments configure the plot so that whenever there is a selection made all the non selected points will be shaded gray with a high transparency the two plots here show the effects before and after a selection before the selection all the circles are drawn with a default appearance after a selection is made with the box select tool the unselected points outside the box are gray and transparent and the selected points inside the box are all shaded red it's also possible to add a hover tool to book a plots the hover tool can be used to drive hover tooltips which we'll see later but can also be used to inform changes to the visual appearance of glyphs the hover tool is a bit more sophisticated let's first take a look at how it's added in this example first we import hover tool from Boca models we create a hover tool instance called hover by calling the hover tool initializer we pass in the arguments tooltips equals none and mode equals H line this configures the hover tool not to display any tooltips and to use a horizontal line under the current mouse position to perform hover inspections we add this tool as well as a crosshair tool to our plot by passing a list containing the hover tool and the string named crosshair to the tools argument to figure having added our hover tool we can now also specify a hover policy this is very similar to the house election policies are specified instead of property names prefixed with selection or non selection we can add property names prefixed with hover in this example we have added hover color equals red to our call to circle accordingly whenever a circle is hovered over it is drawn as red all of the circles are drawn in their normal fashion finally let's look at one more important way to customize visual appearance that has to do with shading groups of data differently let's consider the iris data which has data for three different species of flower what if we want to shade each species differently we could make three separate calls to circle each with a subset of the data and a different single color or we could make a single call to circle with a column of colors that we compute by hand based on the species but there's often a better way which is to configure the color properties with a color mapper that refers to some other column that should already be in the data let's take a closer look first we need to create a categorical color mapper in this example we want to import it from bouquet dot models the color mapper needs to be configured with two arguments the first is a list of the values to map in this case it's the three species names from the data set OSA for Jenica and versicolor next we also have to supply a palette which is just a list of colors we provide an explicit palette red green blue but there are many built-in palettes available in the bouquet top pilots palettes module that can be used as well finally we configure the glyph to use this mapper this is done in a slightly different way than we have seen so far for the color argument we pass a dictionary this dictionary has a key for field with the value being the name of the column to map in this case it's the column name species it also has a key for transform which is the color map for The Associated value this special dictionary the color mapping now happens in the browser automatically looking at the plot here we can see that the points are shaded according to their species value and the color map we provided
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/interactive-data-visualization-with-bokeh at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
We've seen how to create and use glyphs, and specify their basic visual properties. Sometimes, it's useful to be able to alter the appearance of a glyph, for instance when a user makes a selection, or hovers over our glyphs. Let's see how the visual appearance of glyphs can be altered in these situations. Bokeh has several different tools to enable users to select points on a plot using mouse or touch interactions. In this example we create a figure that specifies "tools='box_select,lasso_select'". This configures our plot to have two different kinds of select tools. A "box select" tool that allows you to select points by dragging a rectangular region over the plot. And a "lasso select" tool that allows points to be selected by drawing a free form curve. When points are selected using one of Bokeh's selection tools, how is this fact made visually evident? By default, once a selection is made, Bokeh draws all the NON-selected points with a very low transparency (or alpha value). However it is possible to customize this behavior. To do so we simply pass additional arguments to the glyph method. These arguments are the same visual properties we have already seen, such as "fill_color", except that they either have the prefix "selection_" or "nonselection_".
Let's take a look at the call to "circle" in this example. Here, we specify "selection_color=red". This configures the plot so that whenever a selection is active, all the selected points have a fill_color of red. Note that an argument "color" is just shorthand for specifying "fill_color" and line_color at the same time. Additionally, we specify "nonselection_fill_alpha=0.2" and "nonselection_fill_color=grey". These two arguments configure the plots so that whenever there is a selecti
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: Python for Data
View skill →
🎓
Tutor Explanation
DeepCamp AI