简体   繁体   English

多个matplotlib子图:将熊猫数据框HTML与每个子图交错

[英]multiple matplotlib subplots: interleave pandas dataframe HTML with each subplot

I have a pandas.DataFrame with multiple rows (1 per trade I want to inspect) 我有一个多行的pandas.DataFrame (我要检查的每笔交易1个)

trades = pandas.read_csv(...)

I want to plot each trade on a matplotlib subplot. 我想在matplotlib子图中绘制每笔交易。 I create a pyplot.figure using len(trades) to create sufficient height 我使用len(trades)创建pyplot.figure以创建足够的高度

fig = pyplot.figure(figsize=(40,15 * len(trades)))

I then iterate over each trade and generate a plot 然后,我遍历每笔交易并生成一个图

for i,r in enumerate(trades.iterrows()):
    _, trade = r

    start = trade.open_time  - datetime.timedelta(seconds=30)
    end   = trade.close_time + datetime.timedelta(seconds=30) 

    b = bids[start:end]
    a = asks[start:end]

    ax = fig.add_subplot(len(trades),1,i+1)

    # plot bid/ask
    ax.plot_date(b.index, b, fmt='-',  label='bid')
    ax.plot_date(a.index, a, fmt='-',  label='ask')

    # plot entry/exit markers
    ax.plot(trade.open_time,  trade.open_price,  marker='o', color='b')
    ax.plot(trade.close_time, trade.close_price, marker='o', color='r')

    ax.set_title("Trade {}".format(i+1, fontsize=10)
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")

    ax.legend(loc='best', fontsize='large')

pyplot.show()

# free resources
pyplot.close(fig.number) 

This works great. 这很好。

Now, however, I want to display the rendered HTML of the dataframe for the trade in question. 但是,现在,我想显示有关交易的数据框的渲染HTML。

Since I am doing this in a jupyter notebook, from this SO answer I was able to find the following snippet which will display my dataframe in html: 由于我是在jupyter笔记本中执行此操作的,因此从此SO答案中,我能够找到以下片段,这些片段将以html显示我的数据框:

t = pandas.DataFrame(trades.iloc[i]).T
IPython.display.display(IPython.display.HTML(t.to_html())

I insert this snippet into my loop. 我将此代码段插入循环中。

The problem is that each trade's rendered HTML dataframe is printed one-after-the-other, and then after all of the dataframes have been printed, the plots are printed. 问题在于,每笔交易的渲染HTML数据框都是一个接一个地打印的,然后在所有数据框都打印完之后,打印图。

+-----------+
| dataframe |
+-----------+
+-----------+
| dataframe |
+-----------+
+-----------+
| dataframe |
+-----------+
+------+
|      |
| plot |
|      |
+------+
+------+
|      |
| plot |
|      |
+------+
+------+
|      |
| plot |
|      |
+------+

Given I have created a single large pyplot.figure , and I call pyplot.show() after the loop, this makes sense - inside the loop I output the dataframe HTML, and after the loop I display the plot. 鉴于我已经创建了一个大的pyplot.figure ,并且在循环之后调用了pyplot.show() ,这很有意义-在循环内部我输出数据帧HTML,并在循环之后显示该图。

Question: 题:

How can I interleave the notebook HTML and each subplot? 如何交错笔记本HTML和每个子图?

+-----------+
| dataframe |
+-----------+
+------+
|      |
| plot |
|      |
+------+
+-----------+
| dataframe |
+-----------+
+------+
|      |
| plot |
|      |
+------+
+-----------+
| dataframe |
+-----------+
+------+
|      |
| plot |
|      |
+------+

I believe you need to create three separate figures and call plt.show() within the loop. 我相信您需要创建三个单独的图形并在循环内调用plt.show() Something like this (side note, I don't think one needs pyplot.close using the Jupyter notebook frontend): 这样的事情(注意,我认为 pyplot.close使用Jupyter笔记本前端pyplot.close):

trades = pandas.read_csv(...)

for i, r in enumerate(trades.iterrows()):
    _, trade = r

    start = trade.open_time  - datetime.timedelta(seconds=30)
    end   = trade.close_time + datetime.timedelta(seconds=30) 

    b = bids[start:end]
    a = asks[start:end]

    fig, ax = plt.subplots(figsize=(40, 15))

    # plot bid/ask
    ax.plot_date(b.index, b, fmt='-',  label='bid')
    ax.plot_date(a.index, a, fmt='-',  label='ask')

    # plot entry/exit markers
    ax.plot(trade.open_time,  trade.open_price,  marker='o', color='b')
    ax.plot(trade.close_time, trade.close_price, marker='o', color='r')

    ax.set_title("Trade {}".format(i+1, fontsize=10))
    ax.set_xlabel("Date")
    ax.set_ylabel("Price")

    ax.legend(loc='best', fontsize='large')

    t = pandas.DataFrame(trades.iloc[i]).T
    IPython.display.display(IPython.display.HTML(t.to_html())

    pyplot.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM