0

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)

zape
  • 105
  • 1
  • 6

1 Answers1

1

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)
mosc9575
  • 5,618
  • 2
  • 9
  • 32
  • Hi, do you know why you lose the last point in the dataframe. I am having issues running this for a larger dataset with decimals. The interpolated graph doesnt go untill x = 22. – zape Sep 14 '22 at 11:16
  • I posted a new question here with the full data: https://stackoverflow.com/questions/73716155/pandas-interpolation-creating-a-horizontal-line – zape Sep 14 '22 at 11:35
  • I am sorry, this is something I didn't pay attention. The last value of `range` is not inclusive. You need to use `range(df['x'].min(), df['x'].max()+1))`. – mosc9575 Sep 14 '22 at 12:27