Bokeh – Filtering Data

Bokeh - Filtering Data

In this chapter, we will discuss Bokeh – Filtering Data. Often, you may want to obtain a plot pertaining to a part of data that satisfies certain conditions instead of the entire dataset.

IndexFilter is the simplest type of filter. You have to specify indices of only those rows from the dataset that you want to use while plotting the figure.

The following example demonstrates the use of IndexFilter to set up a CDSView. The resultant figure shows a line glyph between the x and y data series of the ColumnDataSource.

Example’s Of Bokeh – Filtering Data

from bokeh.models import ColumnDataSource, CDSView, IndexFilter
from bokeh.plotting import figure, output_file, show
source = ColumnDataSource(data = dict(x = list(range(1,11)), y = list(range(2,22,2))))
view = CDSView(source=source, filters = [IndexFilter([0, 2, 4,6])])
fig = figure(title = 'Line Plot example', x_axis_label = 'x', y_axis_label = 'y')
fig.circle(x = "x", y = "y", size = 10, source = source, view = view, legend = 'filtered')
fig.line(source.data['x'],source.data['y'], legend = 'unfiltered')
show(fig)

Output

Bokeh - Filtering Data

To choose only those rows from the data source, that satisfy a certain Boolean condition, apply a BooleanFilter.

A typical Bokeh installation consists of a number of sample data sets in the sample data directory. For the following example, we use the unemployment1948 dataset provided in the form of unemployment1948.csv. It stores the year-wise percentage of unemployment in the USA since 1948. We want to generate a plot only for the years 1980 onwards.

from bokeh.models import ColumnDataSource, CDSView, BooleanFilter
from bokeh.plotting import figure, show
from bokeh.sampledata.unemployment1948 import data
source = ColumnDataSource(data)
booleans = [True if int(year) >= 1980 else False for year in
source.data['Year']]
print (booleans)
view1 = CDSView(source = source, filters=[BooleanFilter(booleans)])
p = figure(title = "Unemployment data", x_range = (1980,2020), x_axis_label = 'Year', y_axis_label='Percentage')
p.line(x = 'Year', y = 'Annual', source = source, view = view1, color = 'red', line_width = 2)
show(p)

Output

Bokeh - Filtering Data

To add more flexibility in applying filters, Bokeh provides a CustomJSFilter class with the help of which the data source can be filtered with a user-defined JavaScript function.

The example given below uses the same USA unemployment data. Defining a CustomJSFilter to plot unemployment figures of the year 1980 and after.

from bokeh.models import ColumnDataSource, CDSView, CustomJSFilter
from bokeh.plotting import figure, show
from bokeh.sampledata.unemployment1948 import data
source = ColumnDataSource(data)
custom_filter = CustomJSFilter(code = '''
   var indices = [];

   for (var i = 0; i < source.get_length(); i++){
      if (parseInt(source.data['Year'][i]) > = 1980){
         indices.push(true);
      } else {
         indices.push(false);
      }
   }
   return indices;
''')
view1 = CDSView(source = source, filters = [custom_filter])
p = figure(title = "Unemployment data", x_range = (1980,2020), x_axis_label = 'Year', y_axis_label = 'Percentage')
p.line(x = 'Year', y = 'Annual', source = source, view = view1, color = 'red', line_width = 2)
show(p)

Next Topic – Click Here

This Post Has 13 Comments

Leave a Reply