1. Adding annotations
Now let's discuss annotations, which are aids for making plots easier to read. We've worked with several annotations already, such as titles and labels. We will now focus on box annotations and polygon annotations.
2. Box annotation
Box annotations are rectangular sections in a plot containing specified colors. A typical use-case is where colors correspond with performance metrics, so observations above or below these thresholds can be easily identified.
Here is an example with three box annotations in red, orange, and green depending on values for points per game.
Notice how sparse the green section is? Box annotations allow us to easily spot different frequencies of observations in each section.
3. Adding box annotations
To create the plot we just saw, we first import BoxAnnotation from bokeh-dot-models.
We create one variable for every BoxAnnotation we wish to include. We will add three, displaying red for players averaging 10 points or less, orange for 10 to 20 points, and green for 20 or more points.
We create low_box by calling BoxAnnotation, setting the top equal to the maximum value of the y-axis for the section. We use red as the fill_color and set transparency to zero-point-three.
We repeat this for mid_box, with an additional argument of bottom, which sets the lower y-axis threshold, and assign the color as orange.
The high_box is set as green, and we omit the top argument as we don't require an upper limit for the top section.
We then call fig-dot-add_layout three times, passing each of our BoxAnnotations.
4. Polygon annotation
A polygon annotation creates a polygon of a specified color in our plot. It is particularly useful with line plots as we can highlight a period of interest or draw attention to where a rapid change has occurred.
Here is a line plot displaying stock prices for the company Nvidia, with a polygon annotation highlighting how their stock price increased by almost 70% between 9th of May and 12th of June 2017. Notice how our eyes are instantly drawn to this time period?
5. Adding a polygon annotation
There are a lot of steps we need to take in order to add a Polygon Annotation, so let's walk through them one-by-one.
First, we import datetime as dt and PolyAnnotation from bokeh-dot-models.
We create our desired start and end dates using datetime's datetime function - specifying the dates where the rapid increase in Nvidia's stock price occurred. We then call their timestamp method and multiply by 1000. This converts the dates to floats that will serve as indices for our annotation.
We create start_data and end_data by subsetting our dataset for the respective dates and taking the value of the close column.
Now we call PolyAnnotation, choosing the color and transparency. Next, we use xs to select the x-coordinates of the region to draw, passing a list containing the start_float twice and the end_float twice.
We set y-coordinates with ys, passing the start and end timestamps. We add or subtract to change how far above or below each corner of the region our polygon should display.
Lastly, we add the PolyAnnotation to our figure's layout.
6. Let's practice!
Now we've seen some of the benefits of annotations, let's add some to our plots.