Bokeh – ColumnDataSource

Bokeh - ColumnDataSource

In this chapter, we will discuss Bokeh ColumnDataSource. Most of the plotting methods in API are able to receive data source parameters through the columnDatasource object. It makes sharing data between plots and ‘DataTables’.

NumFocus sponsored by Bokeh project https://numfocus.org/.

Example’s Bokeh ColumnDataSource

Below is the example

from bokeh.models import ColumnDataSource
data = {'x':[1, 4, 3, 2, 5],
   'y':[6, 5, 2, 4, 7]}
cds = ColumnDataSource(data = data)

The following code generates a scatter plot using ColumnDataSource.

from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource
data = {'x':[1, 4, 3, 2, 5],
   'y':[6, 5, 2, 4, 7]}
cds = ColumnDataSource(data = data)
fig = figure()
fig.scatter(x = 'x', y = 'y',source = cds, marker = "circle", size = 20, fill_color = "grey")
show(fig)

Output

Bokeh - ColumnDataSource

Instead of assigning a Python dictionary to ColumnDataSource, we can use a Pandas DataFrame for it.

Let us use ‘test.csv’ (used earlier in this section) to obtain a DataFrame and use it for getting ColumnDataSource and rendering line plot.

from bokeh.plotting import figure, output_file, show
import pandas as pd
from bokeh.models import ColumnDataSource
df = pd.read_csv('test.csv')
cds = ColumnDataSource(df)
fig = figure(y_axis_type = 'log')
fig.line(x = 'x', y = 'pow',source = cds, line_color = "grey")
show(fig)

Output

Bokeh - ColumnDataSource

Next Topic – Click Here

This Post Has One Comment

Leave a Reply