Search code examples
pythonplotlinebokeh

Bokeh plot is empty


I'm trying to make a plot using this data format:

    Date    Adj Close   Volume  Day_Perc_Change Name
0   2018-10-22  7468.629883 2.282400e+09    0.263123    NASDAQ
1   2018-10-23  7437.540039 2.735820e+09    -0.416272   NASDAQ
2   2018-10-24  7108.399902 2.935550e+09    -4.425390   NASDAQ
3   2018-10-25  7318.339844 2.741810e+09    2.953406    NASDAQ
4   2018-10-26  7167.209961 2.964780e+09    -2.065084   NASDAQ

the plan is to make a line plot of the Adj Close prices for each date, and show a hover tooltip of the rest of the data.

I tried plotting like:

s = figure(plot_height=600,
           plot_width=1000,
           title = "Adjacent Closing Prices",
          x_axis_label='Date',
           y_axis_label='Price')
     

    

s.line(source=hsc,
       x='Date',
       y='Adj Close')

output_notebook()
show(s)

with hsc being the columndatasource of the Hang Seng index, but it returns a blank plot. Do I need to preprocess the data into certain formats first?


Solution

  • You can try changing your data types and setting a line width. Here's what worked for me:

    df snippet -

            Date    Adj Close        Volume Day_Perc_Change    Name
    0 2018-10-22  7468.629883  2.282400e+09        0.263123  NASDAQ
    1 2018-10-23  7437.540039  2.735820e+09       -0.416272  NASDAQ
    2 2018-10-24  7108.399902  2.935550e+09       -4.425390  NASDAQ
    3 2018-10-25  7318.339844  2.741810e+09        2.953406  NASDAQ
    4 2018-10-26  7167.209961  2.964780e+09       -2.065084  NASDAQ
    
    
    # Convert Date to datetime and Adj Close to float
    df.loc[: , "Date"] = pd.to_datetime(df.loc[: , "Date"], infer_datetime_format = True)
    df.loc[: , "Adj Close"] = df.loc[: , "Adj Close"].astype(float)
    

    Load Bokeh before creating a figure.

    output_notebook()
    

    EDIT: Adding some tooltips results per @aneroid's comments below + a couple of formatting tweaks.

    # Create HoverTool class object with appropriate formatting for the desired tooltip output.
    # More in the Bokeh inspectors section: https://docs.bokeh.org/en/latest/docs/user_guide/tools.html#inspectors
    ht = HoverTool(
        tooltips = [
            # Formatting similar to strftime().
            # Docs: https://docs.bokeh.org/en/latest/docs/reference/models/formatters.html#bokeh.models.formatters.DatetimeTickFormatter
            ("Date", "@Date{%Y-%m-%d}"),
            
            # Format to two decimal points.  We also have the Hong Kong dollar sign (HK$) at the start.
            ("Adj Close", "HK$@{Adj Close}{%0.2f}"),
        ],
        formatters = {
            "@Date": "datetime",
            "@{Adj Close}": "printf",
        }
    )
    
    # --- Add 'line_width' argument to 's.line()' and 'x_axis_type' argument for appropriate datetime formatting. --- #
    
    s = figure(
        plot_height = 450,
        plot_width = 600,
        title = "Adjacent Closing Prices",
        x_axis_label = "Date",
        y_axis_label = "Price",
        x_axis_type = "datetime", # For x-axis formatting (obviously)
    )
    
    # Add your hovertools object here.
    s.add_tools(ht)
    
    s.line(source = df, x = "Date", y = "Adj Close", line_width = 2)
    
    show(s)
    

    Jupyter Notebook screenshot:

    enter image description here