代码之家  ›  专栏  ›  技术社区  ›  NoahVerner

如何在Python上使用matplotlib在价格走势图上标记2个特定数据点?

  •  0
  • NoahVerner  · 技术社区  · 3 年前

    假设我有以下图表:

    btcusdt price action from 2022-04-01 05:59:59.99 to 2022-05-13 21:59:59.99

    这样的图是使用以下python代码创建的:

    from binance.client import Client
    import pandas as pd
    import matplotlib.pyplot as plt
    
    #personal API key and Secret Key from your Binance account
    
    api_key = "your binance api key"
    secret_key = "your binance secret key"
    
    client = Client(api_key= api_key, api_secret= secret_key, tld= "com")
    
    klines_btcusdt = client.get_historical_klines(symbol="BTCUSDT", interval="1h", start_str = "1648807200000", end_str="1653667199999")
    
    df_btcusdt = pd.DataFrame(klines_btcusdt)
    
    #drop unnecesary columns
    df_btcusdt.drop(5, inplace = True, axis=1)
    df_btcusdt.drop(7, inplace = True, axis=1)
    df_btcusdt.drop(8, inplace = True, axis=1)
    df_btcusdt.drop(9, inplace = True, axis=1)
    df_btcusdt.drop(10, inplace = True, axis=1)
    df_btcusdt.drop(11, inplace = True, axis=1)
    
    # Rename the column names for best practices
    df_btcusdt.rename(columns = { 0 : 'Start Date', 
                              1 : 'Open Price',
                              2 : 'High Price',
                              3 : 'Low Price',
                              4 :'Close Price',
                              6 :'End Date',
                              }, inplace = True)
    
    # Convert Unix Time values to actual dates
    df_btcusdt['Start Date'] = pd.to_datetime(df_btcusdt['Start Date'], unit='ms')
    df_btcusdt['End Date'] = pd.to_datetime(df_btcusdt['End Date'], unit='ms')
    df_btcusdt = df_btcusdt[['End Date','Close Price']]
    df_btcusdt = df_btcusdt.set_index('End Date', inplace=False)
    df_btcusdt = df_btcusdt.astype({'Close Price': 'float'})
    
    #visualising the price
    plt.figure(figsize=(8, 6), dpi=80)
    plt.title('BTCUSDT Price')
    plt.rc('xtick', labelsize = 8)
    plt.plot(df_btcusdt.index[0:], df_btcusdt[0:])
    

    我有兴趣标记两个具体的数据点,它们是: df_btcusdt[0:1] df_btcusdt[1024:1025] ,我的意思是:

                             Close Price
    End Date                            
    2022-04-01 05:59:59.999     44646.16
    
                             Close Price
    End Date                            
    2022-05-13 21:59:59.999     30046.65
    

    但我不知道该怎么做,我尝试将代码的最后一行更改为以下代码:

    plt.plot(df_btcusdt.index[0:], df_btcusdt[0:], markevery = [44646.16, 30046.65], marker="ro")
    

    但得到:

    值错误: markevery=[44646.1630046.65]是可迭代的,但不是有效的numpy花式索引

    它应该抛出这样的东西:

    desired graph

    我可以得到一些帮助吗?

    0 回复  |  直到 3 年前
        1
  •  1
  •   Arne    3 年前

    让我们调用数据帧 df 为了简单起见。它有一个日期时间索引,所以诀窍是查找与要突出显示的两个整数索引对应的日期,并将这些日期用于绘图。

    要绘制线条上方的点,可以使用 zorder 参数。 因此,在折线图命令之后,添加以下内容:

    highlight_dates = df.index[[0, 1024]]
    plt.scatter(highlight_dates, df.loc[highlight_dates, 'Close Price'], 
                color='red', marker='o', zorder=2.5)