{ "cells": [ { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "\n", " \n", " \n", " \n", " \n", "
\n", " \n", " \n", " \n", " \n", "

Bokeh Tutorial

\n", "
\n", "\n", "

02. Styling and Theming

" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "In this chapter we will learn how to configure various visual aspects of our plots, and how to find out more about what an be configured. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Imports and Setup\n", "\n", "First, let's make the standard imports" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from bokeh.io import output_notebook, show\n", "from bokeh.plotting import figure" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "output_notebook()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook uses Bokeh sample data. If you haven't downloaded it already, this can be downloaded by running the following:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "jupyter": { "outputs_hidden": true } }, "outputs": [], "source": [ "import bokeh.sampledata\n", "bokeh.sampledata.download()" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "Before we get started, it's useful to describe how colors and properties are specified in Bokeh. \n", "\n", "# Colors\n", "\n", "There are many places where you may need to specify colors. Bokeh can accept colors in a variety of different ways:\n", "\n", "* any of the [140 named HTML/CSS colors](https://www.w3schools.com/colors/colors_names.asp), e.g ``'green'``, ``'indigo'``\n", "* an RGB(A) hex value, e.g., ``'#FF0000'``, ``'#44444444'``\n", "* a 3-tuple of integers *(r,g,b)* between 0 and 255\n", "* a 4-tuple of *(r,g,b,a)* where *r*, *g*, *b* are integers between 0 and 255 and *a* is a floating point value between 0 and 1\n", "\n", "\n", "\n", "## Properties\n", "\n", "Regardless of how a Bokeh plot is created, styling the visual aspects of the plot can always be accomplished by setting attributes on the Bokeh objects that comprise the resulting plot. Visual properties come in three kinds: line, fill, and text properties. For full information with code and examples see the [Styling Visual Properties](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html) section of the user guide. \n", "\n", "----\n", "\n", "### Line Properties\n", "\n", "Set the visual appearance of lines. The most common are ``line_color``, ``line_alpha``, ``line_width`` and ``line_dash``.\n", "\n", "### Fill Properties\n", "\n", "Set the visual appearance of filled areas: ``fill_color`` and ``fill_alpha``.\n", "\n", "### Text Properties\n", "\n", "Set the visual appearance of lines of text. The most common are ``text_font``, ``text_font_size``, ``text_color``, and ``text_alpha``.\n", "\n", "----\n", "\n", "Sometimes a prefix is used with property names, e.g. to distinguish between different line properties on the same object, or to give a more meaningful name. For example, to set the line width of the plot outline, you would say ``myplot.outline_line_width = 2``. " ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "# Plots\n", "\n", "Many top-level attributes of plots (outline, border, etc.) can be configured. See the [Plots](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#plots) section of the styling guide for full information. \n", "\n", "Here is an example that tweaks the plot outline:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "# create a new plot with a title\n", "p = figure(width=400, height=400)\n", "p.outline_line_width = 7\n", "p.outline_line_alpha = 0.3\n", "p.outline_line_color = \"navy\"\n", "\n", "p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)\n", "\n", "show(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "# EXERCISE Create a plot of your own and customize several plot-level properties\n" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "# Glyphs\n", "\n", "It's also possible to style the visual properties of glyphs (see the [Glyphs](https://docs.bokeh.org/en/latest/docs/user_guide/styling.html#glyphs) section of the styling guide for more information). When using `bokeh.plotting` this is often done when calling the glyph methods:\n", "```python\n", "p.circle(line_color=\"red\", fill_alpha=0.2, ...)\n", "```\n", "But it is also possible to set these properties directly on glyph objects. Glyph objects are found on `GlyphRenderer` objects, which are returned by the `Plot.add_glyph` and `bokeh.plotting` glyph methods like `circle`, `rect`, etc. Let's look at an example:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "p = figure(width=400, height=400)\n", "\n", "# keep a reference to the returned GlyphRenderer\n", "r = p.circle([1,2,3,4,5], [2,5,8,2,7])\n", "\n", "r.glyph.size = 50\n", "r.glyph.fill_alpha = 0.2\n", "r.glyph.line_color = \"firebrick\"\n", "r.glyph.line_dash = [5, 1]\n", "r.glyph.line_width = 2\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "### Selection and non-selection visuals\n", "\n", "You can also control how glyphs look when there are selections involved. The set of \"selected\" points is displayed according to the optional `.selection_glyph` property of a `GlyphRenderer`:\n", "```python\n", "r.selection_glyph = Circle(fill_alpha=1, fill_color=\"firebrick\", line_color=None) \n", "```\n", "When there is a non-empty selection, the set of \"unselected\" points is displayed according to the optional `.nonselection_glyph` property of a `GlyphRenderer`:\n", "```python\n", "r.nonselection_glyph = Circle(fill_alpha=0.2, fill_color=\"grey\", line_color=None) \n", "```\n", "\n", "When using the `bokeh.plotting` interface, it is easier to pass these visual properties to the glyph methods as shown below. The glyph method will create the selection or nonselection glyphs and attach them to the renderer for you. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "p = figure(width=400, height=400, tools=\"tap\", title=\"Select a circle\")\n", "renderer = p.circle([1, 2, 3, 4, 5], [2, 5, 8, 2, 7], size=50,\n", "\n", " # set visual properties for selected glyphs\n", " selection_color=\"firebrick\",\n", "\n", " # set visual properties for non-selected glyphs\n", " nonselection_fill_alpha=0.2,\n", " nonselection_fill_color=\"grey\",\n", " nonselection_line_color=\"firebrick\",\n", " nonselection_line_alpha=1.0)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "It is also possible to specify the visual appearance of glyphs when they are \"inspected\", e.g. by a hover tool. This is accomplished by setting an optional `hover_glyph` on the glyph renderer:\n", "```python\n", "r.hover_glyph = Circle(fill_alpha=1, fill_color=\"firebrick\", line_color=None) \n", "```\n", "Or if using `bokeh.plotting` glyph methods, by passing `hover_fill_alpha`, etc. to the glyph method. Lets look at an example that works together with a `HoverTool` configured for \"hline\" hit-testing." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from bokeh.models.tools import HoverTool\n", "from bokeh.sampledata.glucose import data\n", "\n", "subset = data.loc['2010-10-06']\n", "\n", "x, y = subset.index.to_series(), subset['glucose']\n", "\n", "# Basic plot setup\n", "p = figure(width=600, height=300, x_axis_type=\"datetime\", title='Hover over points')\n", "\n", "p.line(x, y, line_dash=\"4 4\", line_width=1, color='gray')\n", "\n", "cr = p.circle(x, y, size=20,\n", " fill_color=\"grey\", hover_fill_color=\"firebrick\",\n", " fill_alpha=0.05, hover_alpha=0.3,\n", " line_color=None, hover_line_color=\"white\")\n", "\n", "p.add_tools(HoverTool(tooltips=None, renderers=[cr], mode='hline'))\n", "\n", "show(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "# EXERCISE: experiment with standard, selected, hover glyph visual properties\n" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "# Axes\n", "\n", "Next we will take a look at [stlying of Axes](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#axes).\n", "\n", "To style axes, you first must get ahold of `Axis` objects. The simplest way is to use some convenience methods on `Plot`: `axis`, `xaxis`, and `yaxis`. These methods return lists of axis objects:\n", "\n", "```\n", ">>> p.xaxis\n", "[]\n", "```\n", "However, you can set properties on all the elements of the list as if it was a single object:\n", "```\n", "p.xaxis.axis_label = \"Temperature\"\n", "p.axis.major_label_text_color = \"orange\"\n", "```\n", "These are referred to as \"splattable\" lists, and tab completion works on them as well. \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "# EXERCISE Try out tab completion. Type p.xaxis. to see a list of attributes that can be set.\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "## Axis properties\n", "\n", "Axes objects have many configurable properties that afford control over most visual aspects of an axis. These can be grouped by function according to prefix:\n", "\n", "* **axis** [line properties](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#line-properties) e.g `axis_line_width`\n", "\n", "* **axis_label** [text properties](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#text-properties) e.g. `axis_label_text_color`, as well as ``axis_label_standoff``\n", "\n", "* **major_label** [text properties](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#text-properties) e.g. `major_label_text_font_size`, as well as ``major_label_orientation``\n", "\n", "* **major_tick** [line_properties](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#line-properties) e.g. `major_tick_line_dash`, as well as ``major_tick_in`` and ``major_tick_out``\n", "\n", "* **minor_tick** [line properties](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#line-properties) e.g. `minor_tick_line_width`, as well as ``minor_tick_in`` and ``minor_tick_out``\n", "\n", "As a simple first case, let's change the orientation of the major tick labels on both axes of a plot:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from math import pi\n", "\n", "p = figure(width=400, height=400)\n", "p.x([1,2,3,4,5], [2,5,8,2,7], size=10, line_width=2)\n", "\n", "p.xaxis.major_label_orientation = pi/4\n", "p.yaxis.major_label_orientation = \"vertical\"\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "The next example shows customizations on several of the different Axis properties at once:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "p = figure(width=400, height=400)\n", "p.asterisk([1,2,3,4,5], [2,5,8,2,7], size=12, color=\"olive\")\n", "\n", "# change just some things about the x-axes\n", "p.xaxis.axis_label = \"Temp\"\n", "p.xaxis.axis_line_width = 3\n", "p.xaxis.axis_line_color = \"red\"\n", "\n", "# change just some things about the y-axes\n", "p.yaxis.axis_label = \"Pressure\"\n", "p.yaxis.major_label_text_color = \"orange\"\n", "p.yaxis.major_label_orientation = \"vertical\"\n", "\n", "# change things on all axes\n", "p.axis.minor_tick_in = -3\n", "p.axis.minor_tick_out = 6\n", "\n", "show(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "# EXERCISE Create a plot of your own and customize several axis properties\n" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "## Configuring tick labels\n", "\n", "All Bokeh axes have a `formatter` property, whose value is a `TickFormatter` object that Bokeh uses to format the ticks displayed by that axis. Bokeh will configure default tick formatters for numeric, datetime, or categotical axes. But often we would like to customize the appearance of tick labels. This can be accomplished by changing properties on the default formatter that Bokeh chooses, or by replacing the formatter with a new type entirely. \n", "\n", "Let's first look at changing the properties of a default formatter. The default datetime formatter is configured to show *month/day* when the axis is on the scale of days. If would like the also always show the year, we can change the `days` property to a format that includes the year, as done below. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from math import pi\n", "from bokeh.sampledata.glucose import data\n", "\n", "week = data.loc['2010-10-01':'2010-10-08']\n", "\n", "p = figure(x_axis_type=\"datetime\", title=\"Glocose Range\", height=350, width=800)\n", "p.xaxis.formatter.days = '%m/%d/%Y'\n", "p.xaxis.major_label_orientation = pi/3\n", "\n", "p.line(week.index, week.glucose)\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "See the reference guide entry for [DatetimeTickFormatter](https://bokeh.pydata.org/en/latest/docs/reference/models/formatters.html#bokeh.models.formatters.DatetimeTickFormatter) to see other properties that can be updated. \n", "\n", "In addition to the tick formatters that Bokeh will use by default, there are others such as the [`NumeralTickFormatter`](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#numeraltickformatter) that we can configure explicitly. The example below shows how to set a formatter on each axis. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "from bokeh.models import NumeralTickFormatter\n", "\n", "p = figure(height=300, width=800)\n", "p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)\n", "\n", "p.xaxis.formatter = NumeralTickFormatter(format=\"0.0%\")\n", "p.yaxis.formatter = NumeralTickFormatter(format=\"$0.00\")\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "Try experimenting with the format argument and re-execute the cell above.\n", "\n", "There are many other possibilities for controlling tick formatting, including the possibility of supplying a JavaScript snippet to perform arbitrary formatting in the browser. See the [Tick Label Formats](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#tick-label-formats) for more details. \n", "\n", "\n", "It is also possible to customize *where* ticks will be drawn. See the [Tick Locations](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#tick-locations) section of the User's Guide for more information." ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "# Grids\n", "\n", "It is also possible to control the [styling of Grids](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#grids)\n", "\n", "Grids properties in Bokeh have two possible prefixes:\n", "\n", "* **grid** properties (which are [line properties](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#line-properties)) control the \"grid lines\"\n", "* **band** properties (which are [fill properties](https://bokeh.pydata.org/en/latest/docs/user_guide/styling.html#fill-properties)) control shaded bands between grid lines\n", "\n", "In this first example we turn off the vertical grid lines (by setting the line color to None) and set the horizontal grid to be light and dashed." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "p = figure(width=400, height=400)\n", "p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)\n", "\n", "# change just some things about the x-grid\n", "p.xgrid.grid_line_color = None\n", "\n", "# change just some things about the y-grid\n", "p.ygrid.grid_line_alpha = 0.5\n", "p.ygrid.grid_line_dash = [6, 4]\n", "\n", "show(p)" ] }, { "cell_type": "markdown", "metadata": { "Collapsed": "false" }, "source": [ "The next example shows how the \"band\" properties of a plot can be specified" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "p = figure(width=400, height=400)\n", "p.circle([1,2,3,4,5], [2,5,8,2,7], size=10)\n", "\n", "# change just some things about the x-grid\n", "p.xgrid.grid_line_color = None\n", "\n", "# change just some things about the y-grid\n", "p.ygrid.band_fill_alpha = 0.1\n", "p.ygrid.band_fill_color = \"navy\"\n", "\n", "show(p)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "Collapsed": "false" }, "outputs": [], "source": [ "# EXERCISE Create a plot of your own and customize several grid properties\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Next Section" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Click on this link to go to the next notebook: [03 - Data Sources and Transformations](03%20-%20Data%20Sources%20and%20Transformations.ipynb).\n", "\n", "To go back to the overview, click [here](00%20-%20Introduction%20and%20Setup.ipynb)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }