1. Customizing axes
Now let's discuss how to customize axes in Bokeh.
2. A line plot
As mentioned earlier, we often use line plots to visualize changes in observation values over time.
Here we have a dataset called apple, containing information on Apple's market cap, which is their volume of shares outstanding multiplied by the price of each share. We can build a line plot to visualize this data using fig-dot-line.
Notice that the axes don't make much sense? Let's fix this!
3. X-axis type
The simplest approach to displaying time series on the x-axis in Bokeh is to assign datetime to the x_axis_type argument when creating a figure.
Here's the updated output. But suppose we want to use a specific format for our axis ticks, such as displaying months as words and years as numbers, or displaying currency? Bokeh has a couple of useful functions for these use-cases.
4. Formatting options
The first function for changing axis ticks is NumeralTickFormatter. This allows us to customize an axis to display in numeric values using the format argument. This is particularly useful for displaying currency. If we have the value one-thousand-five-hundred and wish to display as dollars with two decimal places, we pass a string containing dollar-zero-point-zero-zero to NumeralTickFormatter's format argument. This would render as displayed at the top of the output column of the NumeralTickFormatter table.
The second function is DatetimeTickFormatter, enabling the display of times and dates along an axis. For example, to display 15th October 2019 as Oct 2019, we call DatetimeTickFormatter and assign the string percent-b percent-Y to the months argument.
This course will use the months argument, but there are several others we can use depending on the time period we would like to visualize.
5. NumeralTickFormatter
To change the y-axis of our line plot, we import NumeralTickFormatter from bokeh-dot-models. After creating our figure, we update the formatter attribute of the first value of fig-dot-y-axis, calling NumeralTickFormatter and completing the format argument. In this case, we set it to display in billions of dollars, with one decimal place so we can easily see each half a billion-dollar increment.
6. DatetimeTickFormatter
To modify the x-axis, we can import DatetimeTickFormatter, also from bokeh-dot-models. Then, we update the formatter attribute of the first value of fig-dot-x-axis, calling DatetimeTickFormatter. We use the months argument, passing a string to specify the format we would like the x-axis to display - choosing three-letter months and four-digit years.
7. The final plot
The plot now displays market cap in billions of dollars and dates as abbreviated months and years.
8. Let's practice!
Now let's put our axes customization skills to practice!