博克的阴谋是空的

2024-06-01 10:02:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用以下数据格式绘制绘图:

    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是恒生指数的columndatasource,但它返回一个空白图。 我需要先将数据预处理成特定格式吗


Tags: 数据绘图closedateplot绘制label数据格式
1条回答
网友
1楼 · 发布于 2024-06-01 10:02:59

您可以尝试更改数据类型并设置线宽。以下是对我有效的方法:

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)

在创建地物之前加载Bokeh

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笔记本屏幕截图:

enter image description here

相关问题 更多 >