Python Tutorial: Customizing glyphs

DataCamp · Beginner ·🛠️ AI Tools & Apps ·6y ago

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 SQL Server Tutorial: Date manipulation
SQL Server Tutorial: Date manipulation
DataCamp
2 R Tutorial: Intermediate Interactive Data Visualization with plotly in R
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
3 R Tutorial: Adding aesthetics to represent a variable
R Tutorial: Adding aesthetics to represent a variable
DataCamp
4 R Tutorial: Moving Beyond Simple Interactivity
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
5 Python Tutorial: Why use ML for marketing? Strategies and use cases
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
6 Python Tutorial: Preparation for modeling
Python Tutorial: Preparation for modeling
DataCamp
7 Python Tutorial: Machine Learning modeling steps
Python Tutorial: Machine Learning modeling steps
DataCamp
8 R Tutorial: The prior model
R Tutorial: The prior model
DataCamp
9 R Tutorial: Data & the likelihood
R Tutorial: Data & the likelihood
DataCamp
10 R Tutorial: The posterior model
R Tutorial: The posterior model
DataCamp
11 R Tutorial: An Introduction to plotly
R Tutorial: An Introduction to plotly
DataCamp
12 R Tutorial: Plotting a single variable
R Tutorial: Plotting a single variable
DataCamp
13 R Tutorial: Bivariate graphics
R Tutorial: Bivariate graphics
DataCamp
14 Python Tutorial: Customer Segmentation in Python
Python Tutorial: Customer Segmentation in Python
DataCamp
15 Python Tutorial: Time cohorts
Python Tutorial: Time cohorts
DataCamp
16 Python Tutorial: Calculate cohort metrics
Python Tutorial: Calculate cohort metrics
DataCamp
17 Python Tutorial: Cohort analysis visualization
Python Tutorial: Cohort analysis visualization
DataCamp
18 R Tutorial: Building Dashboards with flexdashboard
R Tutorial: Building Dashboards with flexdashboard
DataCamp
19 R Tutorial: Anatomy of a flexdashboard
R Tutorial: Anatomy of a flexdashboard
DataCamp
20 R Tutorial: Layout basics
R Tutorial: Layout basics
DataCamp
21 R Tutorial: Advanced layouts
R Tutorial: Advanced layouts
DataCamp
22 Python Tutorial: Time Series Analysis in Python
Python Tutorial: Time Series Analysis in Python
DataCamp
23 Python Tutorial: Correlation of Two Time Series
Python Tutorial: Correlation of Two Time Series
DataCamp
24 Python Tutorial: Simple Linear Regressions
Python Tutorial: Simple Linear Regressions
DataCamp
25 Python Tutorial: Autocorrelation
Python Tutorial: Autocorrelation
DataCamp
26 R Tutorial: The gapminder dataset
R Tutorial: The gapminder dataset
DataCamp
27 R Tutorial: The filter verb
R Tutorial: The filter verb
DataCamp
28 R Tutorial: The arrange verb
R Tutorial: The arrange verb
DataCamp
29 R Tutorial: The mutate verb
R Tutorial: The mutate verb
DataCamp
30 R Tutorial: What is cluster analysis?
R Tutorial: What is cluster analysis?
DataCamp
31 R Tutorial: Distance between two observations
R Tutorial: Distance between two observations
DataCamp
32 R Tutorial: The importance of scale
R Tutorial: The importance of scale
DataCamp
33 R Tutorial: Measuring distance for categorical data
R Tutorial: Measuring distance for categorical data
DataCamp
34 Python Tutorial: Plotting multiple graphs
Python Tutorial: Plotting multiple graphs
DataCamp
35 Python Tutorial: Customizing axes
Python Tutorial: Customizing axes
DataCamp
36 Python Tutorial: Legends, annotations, & styles
Python Tutorial: Legends, annotations, & styles
DataCamp
37 Python Tutorial: Introduction to iterators
Python Tutorial: Introduction to iterators
DataCamp
38 Python Tutorial: Playing with iterators
Python Tutorial: Playing with iterators
DataCamp
39 Python Tutorial: Using iterators to load large files into memory
Python Tutorial: Using iterators to load large files into memory
DataCamp
40 SQL Tutorial: Introduction to Relational Databases in SQL
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
41 SQL Tutorial: Tables: At the core of every database
SQL Tutorial: Tables: At the core of every database
DataCamp
42 SQL Tutorial: Update your database as the structure changes
SQL Tutorial: Update your database as the structure changes
DataCamp
43 Python Tutorial: Classification-Tree Learning
Python Tutorial: Classification-Tree Learning
DataCamp
44 Python Tutorial: Decision-Tree for Classification
Python Tutorial: Decision-Tree for Classification
DataCamp
45 Python Tutorial: Decision-Tree for Regression
Python Tutorial: Decision-Tree for Regression
DataCamp
46 Python Tutorial: Census Subject Tables
Python Tutorial: Census Subject Tables
DataCamp
47 Python Tutorial: Census Geography
Python Tutorial: Census Geography
DataCamp
48 Python Tutorial: Using the Census API
Python Tutorial: Using the Census API
DataCamp
49 R Tutorial: A/B Testing in R
R Tutorial: A/B Testing in R
DataCamp
50 R Tutorial: Baseline Conversion Rates
R Tutorial: Baseline Conversion Rates
DataCamp
51 R Tutorial: Designing an Experiment - Power Analysis
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
52 R Tutorial: Introduction to qualitative data
R Tutorial: Introduction to qualitative data
DataCamp
53 R Tutorial: Understanding your qualitative variables
R Tutorial: Understanding your qualitative variables
DataCamp
54 R Tutorial: Making Better Plots
R Tutorial: Making Better Plots
DataCamp
55 SQL Tutorial: OLTP and OLAP
SQL Tutorial: OLTP and OLAP
DataCamp
56 SQL Tutorial: Storing data
SQL Tutorial: Storing data
DataCamp
57 SQL Tutorial: Database design
SQL Tutorial: Database design
DataCamp
58 Python Tutorial: Introduction to spaCy
Python Tutorial: Introduction to spaCy
DataCamp
59 Python Tutorial: Statistical Models
Python Tutorial: Statistical Models
DataCamp
60 Python Tutorial: Rule-based Matching
Python Tutorial: Rule-based Matching
DataCamp

This video tutorial teaches how to customize glyphs in Bokeh by altering their visual appearance in response to user interactions and using color mappers to shade groups of data differently. The tutorial covers how to add selection and hover tools to a plot, and how to configure the visual appearance of glyphs in response to these interactions. It also shows how to use a color mapper to shade groups of data differently.

Key Takeaways
  1. Import necessary libraries and load data
  2. Create a figure and add selection tools
  3. Configure the visual appearance of glyphs in response to selection
  4. Add a hover tool to the plot
  5. Configure the visual appearance of glyphs in response to hover
  6. Create a categorical color mapper and use it to shade groups of data differently
💡 The tutorial demonstrates how to use Bokeh's selection and hover tools to create interactive visualizations that respond to user interactions, and how to use color mappers to shade groups of data differently.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →