pythonpandasbokehpandas-bokeh

Plot a line on a curve that is undersampled


I was wondering if there was a way to color a line to follow the curve from the user specified input. Example is shown below. The user wants to color a line that starts from x = 11, to x = 14 (see image below for the result). I tried f.ex df.loc[..] where it tries to locate points closest to. But then it just colors it from x = 10 to 15. Anyone have an idea how to solve this? Do I need to add extra points between two points, how would I do that? The user might also add x = 11 to x = 19. Appreciate any help or guidance.

from bokeh.plotting import figure, output_file, show
import pandas as pd

p = figure(width=600, height=600, tools="pan,reset,save")

data = {'x': [1, 2, 3, 6, 10, 15, 20, 22],
        'y': [2, 3, 6, 8, 18, 24, 50, 77]}
  
df = pd.DataFrame(data)
p.line(df.x, df.y)

show(p)

What the result should look like when user inputs x = 11 (start) and x = 14 (end):

![image|540x500](upload://hmcMBr6JzOyQ4Z4H1AfAiHpR5Lu.png)


Solution

  • With pandas you can create an interpolated DataFrame from the original. With this you can add a new line in red.

    from bokeh.plotting import figure, output_notebook, show
    import pandas as pd
    output_notebook()
    p = figure(width=600, height=600, tools="pan,reset,save")
    
    data = {'x': [1, 2, 3, 6, 10, 15, 20, 22],
            'y': [2, 3, 6, 8, 18, 24, 50, 77]}
      
    df = pd.DataFrame(data)
    df_interpolated = (df.copy()
                       .set_index('x')
                       .reindex(index = range(df['x'].min(), df['x'].max()))
                       .reset_index() # optional, you could write 'index' in the second line plot, too.
                       .interpolate()
                      )
    
    p.line(df.x, df.y)
    p.line(df_interpolated.x[11:14], df_interpolated.y[11:14], color="red")
    show(p)