Get startedGet started for free

Communicating with text

1. Communicating with text

Great work so far! In this video, we will look at how we can use text elements to communicate with stakeholders. We will generate a single plot, then update it with custom text elements, including a title, a legend title, and even an interactive legend!

2. Our plot

Here is our initial plot displaying points versus assists for NBA players, broken down by the conference they play in. It looks good, but some simple edits will make it great.

3. Adding a title

Firstly, we can add a title explaining what this plot represents. We do this by completing the title argument when creating the figure, using a string of our desired title. Notice the title alignment is to the left by default, and the font is in bold.

4. Customizing the title

That's a good start, but what if we want the title to really stand out? We can update the title's font size using the figure's title-dot-text_font_size attribute, specifying a string containing a number followed by px. Px is short for pixels, so 30px means a font size of 30 pixels. We can also change the title alignment by updating the title-dot-align attribute, passing a string. By assigning center, the title displays above the middle of the figure. There is an issue; however, the current format of our legend means that stakeholders need to know East and West are the two conferences in the NBA. The legend is also obscuring some values.

5. Modifying the legend

We can add a title to our legend by passing a string to the figure's legend-dot-title attribute. We can also move the legend, assigning bottom_right to figure's legend-dot-location. Here are the options we can use to move the location of a legend.

6. Legend title's font style

By default, the font type of a legend's title is italic. We can update it by passing a string to the figure's legend-dot-title_text_font_style attribute. Here are the available options.

7. Legend title font style

Here we have changed the font style of the legend's title to normal, matching the style used for the labels displayed in the legend.

8. Displaying an interactive legend

There is one more thing we can do to really make this plot stand out. Bokeh allows us to hide observations by clicking on their label in the legend, using only one line of code! We update the figure's legend-dot-click_policy attribute using the string hide. With this, our stakeholders can temporarily remove groups of observations from a plot - imagine how useful this could be if we are visualizing many categories. That looks pretty impressive, right?

9. The dataset

For the next few exercises, we will be working for a bakery, who have provided a dataset containing sales data from their store across 2016 and 2017. Each row represents a transaction. There are six columns representing transaction number, the item sold, time of day, type of day, date, and the cost of the purchase.

10. Let's practice!

Now, let's use text elements to enhance our plots!