1. Highlighting and contrasting
Welcome back! Now we will discuss highlighting and contrasting, a visualization skill for drawing attention to specific elements in our plots! We'll explore the use of dynamic glyph sizing and color mapping.
2. Vectorizing glyph size
Suppose we want to increase glyph size as our observation values increase? We can do this by dividing the column our glyph size maps to by a number to create a variable called sizes. There isn't a specific rule on what number to use; we simply experiment with how our glyphs render using different calculations.
We then pass sizes to fig-dot-circle's radius argument, not to be confused with the size argument.
3. Different sizes
The output of our previous script is on the left.
On the right, we divided the points column by 25 instead of 50. It is difficult to interpret; hence dividing by 50 is more appropriate.
4. Palettes
We have changed the color of glyphs previously. Bokeh actually allows us to apply color mapping based on our observations' values or categories.
Bokeh has over 400 palettes, such as Inferno from Matplotlib, and a usability palette for people who are colorblind. There are versions of each palette depending on how many colors you require, as the images show.
We can import and execute palettes surrounded by double-underscores to see the available palettes. Here are the first eight.
5. Color mapping and color bars
Linear color mapping is used to assign glyph colors based on observation values. For this, generally, we use sequential palettes, meaning colors get darker or lighter as the values change.
We can also include a color bar, which is used to display the relationship between colors and values. Here is what we will build, showing a color bar on the right and glyphs that change color in line with an increase in points.
6. Linear color mapping
To build our plot, we import linear_cmap from bokeh-dot-transform.
We use yellow-orange-brown-eight from bokeh-dot-palettes, which has eight shades of yellow and brown. We also import ColorBar from bokeh-dot-models.
We call linear_cmap, assigning the column name for color mapping to the field_name. The palette is the color palette we wish to use. The low and high arguments take the smallest and largest values from our mapping column, in this case, points, so we assign using Python's min and max functions.
We pass our mapper to the fill_color and line_color arguments when adding glyphs. Next, we call ColorBar, passing the mapper's transform key, and set its width to 8 pixels.
We call fig-dot-add_layout and pass our ColorBar along with the desired location, in this case adding it to the right of our figure.
7. Linear color map scatter plot
See how the colors get lighter as points increase?
8. Factor color mapping
We can also use factor color mapping, where each category is assigned a different color. This allows us to create a legend without adding glyphs for each category individually!
We import factor_cmap from bokeh-dot-transform, along with a categorical color palette. We use Category10_5 as there are five categories, one for each basketball position.
We call factor_cmap when setting fig-dot-circle's fill_color argument. Inside, we state the column name, set the palette, and pass our list of factors to the factors argument. In this case, the list of positions.
By using factor_cmap, we are able to include the legend_field argument when adding glyphs, specifying the column used for our factor_cmap.
9. Color categorized bar plot
That looks great and was much easier than adding five sets of glyphs, right?
10. Let's practice!
Now let's use Bokeh for highlighting and contrasting in our plots!