【问题标题】:multiple matplotlib subplots: interleave pandas dataframe HTML with each subplot多个 matplotlib 子图:将 pandas 数据框 HTML 与每个子图交错
【发布时间】:2019-04-24 13:57:57
【问题描述】:

我有一个包含多行的pandas.DataFrame(我想检查每笔交易 1 个)

trades = pandas.read_csv(...)

我想在 matplotlib 子图上绘制每笔交易。我使用len(trades) 创建了一个pyplot.figure 以创建足够的高度

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

然后我遍历每笔交易并生成一个图

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) 

这很好用。

但是,现在我想显示相关交易的数据框的渲染 HTML。

由于我在 jupyter 笔记本中执行此操作,因此从 this SO answer 我能够找到以下 sn-p,它将以 html 显示我的数据框:

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

我将这个 sn-p 插入到我的循环中。

问题是每笔交易的渲染 HTML 数据帧都是一个接一个地打印,然后在所有数据帧都打印完之后,再打印图表。

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

鉴于我创建了一个大的pyplot.figure,并且我在循环之后调用pyplot.show(),这是有道理的 - 在循环内部我输出数据帧 HTML,在循环之后我显示情节。

问题:

如何交错笔记本 HTML 和每个子图?

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

【问题讨论】:

    标签: python pandas matplotlib jupyter-notebook


    【解决方案1】:

    我相信您需要创建三个单独的数字并在循环内调用plt.show()。像这样的东西(旁注,我不认为需要pyplot.close使用Jupyter笔记本前端):

    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()
    

    【讨论】:

    • 该死的,太明显了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-24
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2018-08-06
    • 1970-01-01
    相关资源
    最近更新 更多