pandas 画图b图标在a图上

def show(a,b,bk,exchange,symbol):
    plt.rcParams['font.family'] = 'sans-serif'

    # print(1)
    fig, ax = plt.subplots(nrows=4,ncols=1,figsize=(30,30))
    fig.suptitle(f"{exchange}-{symbol}",fontsize=40,va='bottom')

    ax[0].plot(a['date'], a['ask_price'], label='ask_price')
    ax[0].plot(a['date'], a['bid_price'], label='bid_Price')

    ax[1].plot(a['date'], a['ask_price'], label='ask_price')
    ax[1].plot(a['date'], a['bid_price'], label='bid_Price')
    # 添加图例和标签

    # print(2)
    # 设置x轴日期格式
    # date_formatter = mdates.DateFormatter('%Y-%m-%d %H:%M:%S')
    # ax[0].xaxis.set_major_formatter(date_formatter)
    # fig.autofmt_xdate()  # 自动格式化日期标签以避免重叠
    # ax[1].xaxis.set_major_formatter(date_formatter)
    # fig.autofmt_xdate()  # 自动格式化日期标签以避免重叠
    # 绘制b的横线
    # print(3)
    for index, row in b.iterrows():
        c = 'grey'
        if(row['amount'] > 0):
            c = 'r'
            if(row['direction'] == 'buy'):
                c = 'blue'
            ax[0].annotate(row['price'],xy=(row['start'],row['price']),xytext=(row['start'],row['price']))
            #小于1秒标星,大于1秒画线
            if(row['close_time'] - row['create_time'] < 1000000):
                ax[0].add_artist(Line2D([row['start']], [row['price']], marker='*', color=c, markersize=10))
            else:
                ax[0].hlines(y=row['price'], xmin=row['start'], xmax=row['end'], colors=c,linewidth=10)

        else:
            ax[0].hlines(y=row['price'], xmin=row['start'], xmax=row['end'], colors=c)#linestyles='dashed'
    # print(4)
    y1_min, y1_max = ax[0].get_ylim()
    x1_min, x1_max = ax[0].get_xlim()
    
    ax[0].set_title("shipan")

    # 显示图形
    ax[0].legend()
    
    for index, row in bk.iterrows():
        c = 'grey'
        if(row['amount'] > 0):
            c = 'r'
            if(row['side'] == 'bid'):
                c = 'blue'
            ax[1].annotate(row['price'],xy=(row['start'],row['price']),xytext=(row['start'],row['price']))
            #小于1秒标星,大于1秒画线
            if(row['close_time'] - row['create_time'] < 1000000):
                ax[1].add_artist(Line2D([row['start']], [row['price']], marker='*', color=c, markersize=10))
            else:
                ax[1].hlines(y=row['price'], xmin=row['start'], xmax=row['end'], colors=c,linewidth=10)

        else:
            ax[1].hlines(y=row['price'], xmin=row['start'], xmax=row['end'], colors=c)
    
    # print(5)
    ax[1].set_ylim(y1_min, y1_max)
    ax[1].set_title("huice")

    # 显示图形
    ax[1].legend()
    
    b_ask = b[b['side'] == 'ask']
    b_bid = b[b['side'] == 'bid']
    
    bk_ask = bk[bk['side'] == 'ask']
    bk_bid = bk[bk['side'] == 'bid']
    
    ax[2].plot(b_ask['start'], b_ask['rb'], label='order_ask_rb')
    ax[2].plot(bk_ask['start'], bk_ask['rb_side'], label='bk_ask_rb')
    ax[2].set_xlim(x1_min, x1_max)
    ax[2].set_title("order_ask_rb:shipan     bk_ask_rb:huice")
    ax[2].legend()
    ax[3].plot(b_bid['start'], b_bid['rb'], label='order_bid_rb')
    ax[3].plot(bk_bid['start'], bk_bid['rb_side'], label='bk_bid_rb')
    ax[3].set_xlim(x1_min, x1_max)
    ax[3].set_title("order_ask_rb:shipan     bk_ask_rb:huice")
    ax[3].legend()
    # print(6)
    plt.show()
    

你可能感兴趣的:(python,pandas,python,开发语言)