Reddy2810
Reddy2810

Reputation: 310

how to plot a single line with different types of line dash using bokeh?

I am trying to plot the line for a set of points. Currently, I have set of points as Column names X, Y and Type in the form of a data frame. Whenever the type is 1, I would like to plot the points as dashed and whenever the type is 2, I would like to plot the points as a solid line. Currently, I am using for loop to iterate over all points and plot each point using plt.dash. However, this is slowing down my run time since I want to plot more than 40000 points. So, is an easy way to plot the line overall points with different line dash type?

Upvotes: 0

Views: 935

Answers (1)

Tony
Tony

Reputation: 8297

You could realize it by drawing multiple line segments like this (Bokeh v1.1.0)

import pandas as pd
from bokeh.plotting import figure, show
from bokeh.models import ColumnDataSource, Range1d, LinearAxis

line_style = {1: 'solid', 2: 'dashed'}

data = {'name': [1, 1, 1, 2, 2, 2, 1, 1, 1, 1],
        'counter': [1, 2, 3, 3, 4, 5, 5, 6, 7, 8],
        'score': [150, 150, 150, 150, 150, 150, 150, 150, 150, 150],
        'age': [20, 21, 22, 22, 23, 24, 24, 25, 26, 27]}
df = pd.DataFrame(data)

plot = figure(y_range = (100, 200))
plot.extra_y_ranges = {"Age": Range1d(19, 28)}
plot.add_layout(LinearAxis(y_range_name = "Age"), 'right')

for i, g in df.groupby([(df.name != df.name.shift()).cumsum()]):
    source = ColumnDataSource(g)
    plot.line(x = 'counter', y = 'score', line_dash = line_style[g.name.unique()[0]], source = source)
    plot.circle(x = 'counter', y = 'age', color = "blue", size = 10, y_range_name = "Age", source = source)

show(plot)

enter image description here

Upvotes: 1

Related Questions