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)
trades = pandas.read_csv(...)
I want to plot each trade on a matplotlib subplot. I create a pyplot.figure
using len(trades)
to create sufficient height
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.
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:
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.
+-----------+
| 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.
Question:
How can I interleave the notebook HTML and each subplot?
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
python pandas matplotlib jupyter-notebook
add a comment |
I have a pandas.DataFrame
with multiple rows (1 per trade I want to inspect)
trades = pandas.read_csv(...)
I want to plot each trade on a matplotlib subplot. I create a pyplot.figure
using len(trades)
to create sufficient height
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.
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:
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.
+-----------+
| 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.
Question:
How can I interleave the notebook HTML and each subplot?
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
python pandas matplotlib jupyter-notebook
add a comment |
I have a pandas.DataFrame
with multiple rows (1 per trade I want to inspect)
trades = pandas.read_csv(...)
I want to plot each trade on a matplotlib subplot. I create a pyplot.figure
using len(trades)
to create sufficient height
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.
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:
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.
+-----------+
| 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.
Question:
How can I interleave the notebook HTML and each subplot?
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
python pandas matplotlib jupyter-notebook
I have a pandas.DataFrame
with multiple rows (1 per trade I want to inspect)
trades = pandas.read_csv(...)
I want to plot each trade on a matplotlib subplot. I create a pyplot.figure
using len(trades)
to create sufficient height
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.
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:
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.
+-----------+
| 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.
Question:
How can I interleave the notebook HTML and each subplot?
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
+-----------+
| dataframe |
+-----------+
+------+
| |
| plot |
| |
+------+
python pandas matplotlib jupyter-notebook
python pandas matplotlib jupyter-notebook
asked Nov 21 '18 at 22:51
Steve LorimerSteve Lorimer
12.7k1267142
12.7k1267142
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I believe you need to create three separate figures and call plt.show()
within the loop. Something like this (side note, I don't think one needs pyplot.close
using the Jupyter notebook frontend):
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()
1
damn, it's so obvious! Thank you!
– Steve Lorimer
Nov 21 '18 at 22:59
1
@SteveLorimer, you're welcome, and happy coding!
– Peter Leimbigler
Nov 21 '18 at 23:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421543%2fmultiple-matplotlib-subplots-interleave-pandas-dataframe-html-with-each-subplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I believe you need to create three separate figures and call plt.show()
within the loop. Something like this (side note, I don't think one needs pyplot.close
using the Jupyter notebook frontend):
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()
1
damn, it's so obvious! Thank you!
– Steve Lorimer
Nov 21 '18 at 22:59
1
@SteveLorimer, you're welcome, and happy coding!
– Peter Leimbigler
Nov 21 '18 at 23:00
add a comment |
I believe you need to create three separate figures and call plt.show()
within the loop. Something like this (side note, I don't think one needs pyplot.close
using the Jupyter notebook frontend):
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()
1
damn, it's so obvious! Thank you!
– Steve Lorimer
Nov 21 '18 at 22:59
1
@SteveLorimer, you're welcome, and happy coding!
– Peter Leimbigler
Nov 21 '18 at 23:00
add a comment |
I believe you need to create three separate figures and call plt.show()
within the loop. Something like this (side note, I don't think one needs pyplot.close
using the Jupyter notebook frontend):
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()
I believe you need to create three separate figures and call plt.show()
within the loop. Something like this (side note, I don't think one needs pyplot.close
using the Jupyter notebook frontend):
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()
answered Nov 21 '18 at 22:57
Peter LeimbiglerPeter Leimbigler
4,5331416
4,5331416
1
damn, it's so obvious! Thank you!
– Steve Lorimer
Nov 21 '18 at 22:59
1
@SteveLorimer, you're welcome, and happy coding!
– Peter Leimbigler
Nov 21 '18 at 23:00
add a comment |
1
damn, it's so obvious! Thank you!
– Steve Lorimer
Nov 21 '18 at 22:59
1
@SteveLorimer, you're welcome, and happy coding!
– Peter Leimbigler
Nov 21 '18 at 23:00
1
1
damn, it's so obvious! Thank you!
– Steve Lorimer
Nov 21 '18 at 22:59
damn, it's so obvious! Thank you!
– Steve Lorimer
Nov 21 '18 at 22:59
1
1
@SteveLorimer, you're welcome, and happy coding!
– Peter Leimbigler
Nov 21 '18 at 23:00
@SteveLorimer, you're welcome, and happy coding!
– Peter Leimbigler
Nov 21 '18 at 23:00
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421543%2fmultiple-matplotlib-subplots-interleave-pandas-dataframe-html-with-each-subplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown