散景图为空

Bokeh plot is empty

我正在尝试使用这种数据格式绘制图表:

    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

计划绘制每个日期的调整收盘价线图,并显示其余数据的悬停工具提示。

我试过像这样绘制:

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)

hsc 是恒生指数的列数据源,但它returns 是一个空白图。 我需要先将数据预处理成特定格式吗?

您可以尝试更改数据类型并设置线宽。这是对我有用的:

df 片段 -

        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)

在创建图形之前加载散景。

output_notebook()

编辑:根据下面@aneroid 的评论添加一些工具提示结果 + 一些格式调整。

# 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 屏幕截图: