【发布时间】: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