1. Adding style
Now we are familiar with Bokeh, we're going to look at how we can customize the style of our plots to make them really stand out!
2. Bokeh themes
One way to change the look of our plot is to use a theme. Bokeh has several built-in but also accepts custom themes from YAML or JSON files. Custom themes are outside of the scope of this course and will not be covered, but the documentation is linked in the slide for reference.
Bokeh has five built-in themes: caliber, dark minimal, light minimal, night sky, and contrast.
3. Using themes
To set a theme, first, we import curdoc from bokeh-dot-io. We then call curdoc, followed by its dot-theme attribute, and assign it to one of the pre-defined themes, written as a string. We set the theme before creating our plot. Here is an example of the dark minimal theme.
4. Subsetting data
So far, we have worked with a single dataset, but we can also visualize multiple sets. We can subset our NBA dataset to create two DataFrames representing players in the Eastern and Western conferences, respectively. Here we display the column names and shape of the two DataFrames.
5. Customizing color
We can customize the color for each DataFrame to visualize differences between groups. For each DataFrame, we use fig-dot-circle, passing the DataFrame to add it to the figure, and include an additional color keyword argument to specify the desired color as a string.
In the example shown, the Eastern Conference players are displayed in blue and Western Conference players are displayed in red.
6. Adding a legend
The last plot looks great, but only because we know what the colors represent. We'll want to add a legend to clearly communicate with stakeholders about what the observations represent.
To add a legend, we pass the legend_label argument to fig-dot-circle and specify the text we want to use for the legend as a string.
Here is the same plot, with a legend added in the bottom right corner. Now we can clearly see what each color means.
7. Glyph types
So far, we've used circle glyphs to visualize observations in scatter plots. However, we may wish to further distinguish observations from different groups. Bokeh makes this easy, with a variety of glyphs available; for example, we can use square, triangle, and diamond glyphs as shown on the left.
All glyphs can be called in the same way as fig-dot-circle. For example, fig-dot-square produces square markers as shown on the right.
8. Multiple glyphs
We can create a plot with as many glyphs as we wish by specifying the type each time we add glyphs to a figure. Here we have reproduced our plot of assists versus points for East and West conference NBA players. We use fig-dot-diamond to generate diamond glyphs representing Eastern Conference NBA players, and fig-dot-triangle to add triangle glyphs representing Western Conference players.
9. The dataset
In this chapter, we will work with a real estate company. They have provided us with a dataset about the housing market in Melbourne, Australia, called melb. It contains one property per row and columns with information including the number of rooms, type of property, date of sale, price (in Australian Dollars), and distance to the Central Business District.
There are thirteen columns, and thirteen-thousand, five-hundred, and eighty rows.
10. Let's practice!
We've covered a lot in this video, now let's add some style to our plots!