Paul H Paul H - 4 months ago 89
Python Question

Categorical axis with a bokeh area chart

Question



Is it currently possible to use a categorical axis with a bokeh
Area
chart?

Details



I'm trying to plot an area chart with a categorical x-axis using the high-level
bokeh.charts
API.

Example



If set up bokeh and a dataframe like this:

import numpy
import pandas
from bokeh import charts, plotting, models
plotting.output_notebook()

_, blue, _, green = bokeh.palettes.Paired4

datalist = [
{'month': 'Oct', 'rain': 107., 'snow': 0.0, 'wy_month': 0},
{'month': 'Nov', 'rain': 23.6, 'snow': 0.8, 'wy_month': 1},
{'month': 'Dec', 'rain': 31.9, 'snow': 30.5, 'wy_month': 2},
{'month': 'Jan', 'rain': 44.6, 'snow': 31.1, 'wy_month': 3},
{'month': 'Feb', 'rain': 49.6, 'snow': 31.7, 'wy_month': 4},
{'month': 'Mar', 'rain': 13.6, 'snow': 4.2, 'wy_month': 5},
{'month': 'Apr', 'rain': 107.2, 'snow': 1.6, 'wy_month': 6},
{'month': 'May', 'rain': 77.0, 'snow': 0.0, 'wy_month': 7},
{'month': 'Jun', 'rain': 108., 'snow': 0.0, 'wy_month': 8},
{'month': 'Jul', 'rain': 216., 'snow': 0.0, 'wy_month': 9},
{'month': 'Aug', 'rain': 76.8, 'snow': 0.0, 'wy_month': 10},
{'month': 'Sep', 'rain': 76.4, 'snow': 0.0, 'wy_month': 11},
]

data = pandas.DataFrame(datalist)


I can do:

bar = charts.Bar(data=data, values='rain', label='month',
color=[blue], width=600, height=300)
plotting.show(bar)

scatter = charts.Scatter(data=data, x='month', y='rain',
color=[blue], width=600, height=300)
plotting.show(scatter)


and I get:

bars

and

scatter

What I've tried



But if I do that same with an area chart:

area = charts.Area(data=data, y='rain', x='month', stack=True,
color=blue, width=600, height=300)
plotting.show(area)


I get:

enter image description here

Other efforts



Removing the explicit definition of
x
makes the areas show up, but with a numeric axis (0 - 12).

area = charts.Area(data=data, y='rain', stack=True,
color=blue, width=600, height=300)


Specifying an
x_range
as either a list or
bokeh.models.ranges.FactorRange
seems to have no effect (with and without
x='month'
).

Answer

One option would be to make custom tick labels for your axes. Modifying from the answer here - How do I use custom labels for ticks in Bokeh?:

First up we do our imports and create a dictionary to store our modified labels (using Jupyter notebook):

import numpy
import pandas
from bokeh import charts, plotting, models
import bokeh
from bokeh.models.formatters import TickFormatter, String, List, Dict, Int
from bokeh.models import FixedTicker
plotting.output_notebook()

_, blue, _, green = ['#a6cee3', '#1f78b4', '#b2df8a', '#33a02c']

datalist = [
    {'month': 'Oct', 'rain': 107., 'snow': 0.0, 'wy_month': 0},
    {'month': 'Nov', 'rain': 23.6, 'snow': 0.8, 'wy_month': 1},
    {'month': 'Dec', 'rain': 31.9, 'snow': 30.5, 'wy_month': 2},
    {'month': 'Jan', 'rain': 44.6, 'snow': 31.1, 'wy_month': 3},
    {'month': 'Feb', 'rain': 49.6, 'snow': 31.7, 'wy_month': 4},
    {'month': 'Mar', 'rain': 13.6, 'snow': 4.2, 'wy_month': 5},
    {'month': 'Apr', 'rain': 107.2, 'snow': 1.6, 'wy_month': 6},
    {'month': 'May', 'rain': 77.0, 'snow': 0.0, 'wy_month': 7},
    {'month': 'Jun', 'rain': 108., 'snow': 0.0, 'wy_month': 8},
    {'month': 'Jul', 'rain': 216., 'snow': 0.0, 'wy_month': 9},
    {'month': 'Aug', 'rain': 76.8, 'snow': 0.0, 'wy_month': 10},
    {'month': 'Sep', 'rain': 76.4, 'snow': 0.0, 'wy_month': 11},

]

data = pandas.DataFrame(datalist)
label_data = {x['wy_month']:x['month'] for x in datalist}

Now we define our custom label class (and associated JS code):

JS_CODE =  """
        _ = require "underscore"
        Model = require "model"
        p = require "core/properties"
        class FixedTickFormatter extends Model
          type: 'FixedTickFormatter'
          doFormat: (ticks) ->
            labels = @get("labels")
            return (labels[tick] ? "" for tick in ticks)
          @define {
            labels: [ p.Any ]
          }
        module.exports =
          Model: FixedTickFormatter
    """

class FixedTickFormatter(TickFormatter):

    labels = Dict(Int, String, help="""
    A mapping of integer ticks values to their labels.
    """)

    __implementation__ = JS_CODE

And plot your chart with modified axes:

area = charts.Area(data=data, y='rain', x='wy_month', stack=True,
                   color=blue, width=600, height=300)
area.xaxis[0].formatter = FixedTickFormatter(labels=label_data)
area.xaxis[0].ticker = FixedTicker(ticks=sorted(label_data.keys()))
plotting.show(area)

Here is the final plot I get:

Bokeh area plot

Comments