1

I have a data frame read in from a .csv with the following format:

version, 2x8x8, 2x8x10, 2x8x12, ...
v1.0.0,  10.2,  9.2,    8.2,
v1.0.1,  11.3,  10.4,   10.2,
v1.0.2,  9.5,   9.3,    9.1,  
...

I want to plot this data frame as a multiline plot in bokeh where each column is its own line. The x axis being version num, and the y values being the content of the column excluding the header.

I have tried referencing the bokeh docs themselves but I cannot find the best way to extract the columns as "lists of lists" as bokeh expects.

# produces empty plot
f = figure(title='Example title')
versions = list(df['version'])
values = [list(df['2x8x8']), list(df['2x8x10']), ...]
f.multi_line(xs=versions, ys=values)

When I attempt to use the alternate ColumnDataSource approach, also specified in the bokeh docs, the plot takes all my y values and makes a new line for each.

# produces plot seen below
df = pd.read_csv(my.csv)
data_source = ColumnDataSource(df)
f = figure(title="Example")
f.line(x='version', y='2x8x8', source=data_source, line_width=2, legend='2x8x8')
f.line(x='version', y='2x8x10', source=data_source, line_width=2, legend='2x8x10')
f.xaxis.axis_label = 'version'

Any help is greatly appreciated!

enter image description here

2 Answers 2

2

I think this is what you want (tested on Bokeh v1.0.4):

import pandas as pd
import numpy as np
from bokeh.palettes import Spectral11
from bokeh.plotting import figure, show

toy_df = pd.DataFrame(data = {'version': ['v1.0.0', 'v1.0.1', 'v1.0.2', 'v1.0.3'],
                              '2x8x8': [10.2, 11.3, 9.5, 10.9],
                              '2x8x10': [9.2, 10.4, 9.3, 9.9],
                              '2x8x12': [8.2, 10.2, 9.1, 11.1]}, columns = ('version', '2x8x8' , '2x8x10', '2x8x12'))

numlines = len(toy_df.columns)
mypalette = Spectral11[0:numlines]

p = figure(width = 500, height = 300, x_range = toy_df['version'])
p.multi_line(xs = [toy_df['version'].values] * numlines,
             ys = [toy_df[name].values for name in toy_df],
             line_color = mypalette,
             line_width = 5)
show(p)

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this works, any idea how to add a legend? I know its tricky with a multiline plot in bokeh.
2

Another version including labels. This is a different approach using explicitly ColumnDataSource instead of pandas DataFrame.

Please note that if you want to use p.legend.click_policy = "hide" to toggle visibility or mute separate lines then you should rather use line glyph instead of multi_line. This code works for Bokeh v1.0.4

from bokeh.palettes import Spectral11
from bokeh.plotting import figure, show
from bokeh.models import Legend, ColumnDataSource

versions = ['v1.0.0', 'v1.0.1', 'v1.0.2', 'v1.0.3']
data = {'version': [versions] * 3,
        'values': [[10.2, 11.3, 9.5, 10.9],
                   [9.2, 10.4, 9.3, 9.9],
                   [8.2, 10.2, 9.1, 11.1]],
        'columns': ['2x8x8', '2x8x10', '2x8x12'],
        'color': Spectral11[0:3] }

source = ColumnDataSource(data)

p = figure(width = 500, height = 300, x_range = versions)
p.multi_line(xs = 'version',
             ys = 'values',
             color = 'color',
             legend = 'columns',
             line_width = 5,
             source = source)
show(p)

Result:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.