New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get interactive plots in Jupyter Notebook #1615

Merged
merged 2 commits into from May 7, 2018

Conversation

Projects
None yet
5 participants
@krischer
Member

krischer commented Dec 7, 2016

Hello,

If I run the following code in a Jupyter Notebook Python3 cell I get a static image plot instead of the more useful interactive plot.

%matplotlib notebook

import matplotlib.pyplot as plt
import numpy as np

import obspy
st = obspy.read()
st.plot()

If in a following cell I execute

t = np.arange(0.0, 2.0, 0.01)
s = np.sin(2*np.pi*t)
plt.plot(t, s)

I get the interactive plot, as expected.
Is there a problem with ObsPy?

I'm using obspy-python3 compiled from a reasonably up-to-date git snapshot on a Debian system.

python3-obspy 1.0.2.post0+794.ge647893002
jupyter-notebook 4.2.3
python3-jupyter-client 4.4.0
python3-jupyter-core 4.2.0
python3 3.5.2

Thanks!

@krischer

This comment has been minimized.

Member

krischer commented Dec 6, 2016

Interesting. I can reproduce this behavior.

I'm fairly sure that it did work in the past so maybe it is a combination of some new changes to either matplotlib or jupyter and the way ObsPy plots things? We'll look into it.

@megies megies added the bug label Dec 6, 2016

@megies megies added this to the 1.1.1 milestone Dec 6, 2016

@QuLogic

This comment has been minimized.

Member

QuLogic commented Dec 6, 2016

Use st.plot(handle=True) to return the figure handle and not have ObsPy automatically close the figure.

@vvug

This comment has been minimized.

vvug commented Dec 6, 2016

If I try that, I get two plots instead of one. Strange.
Anyway it did work in the past, as @krischer recalls.
Keep in mind that this is a Jupyter Notebook issue, not an ipython issue.

@QuLogic

This comment has been minimized.

Member

QuLogic commented Dec 6, 2016

That's because the notebook will render the value on the last line and you didn't assign the result of st.plot to anything. Either use fig = st.plot(handle=True) or st.plot(handle=True); to suppress that behaviour.

@krischer

This comment has been minimized.

Member

krischer commented Dec 6, 2016

Hmm...can we just always return the handle? Any downside to that? Except from a double plot.

@vvug

This comment has been minimized.

vvug commented Dec 6, 2016

OK, the workaround (adding a semicolon) works, still it is not the behavior the user expects.
I still think this issue deserves the "bug" label.

@krischer

This comment has been minimized.

Member

krischer commented Dec 6, 2016

The dual plots are actually kind of expected. ObsPy has to auto-call plt.show() - otherwise users would see nothing in a normal shell. If the plot method returns a figure, ipython/jupyter will show it again. I'm not sure there is anything we can do against that.

But plotting should definitely work, even without handle=True.

@vvug

This comment has been minimized.

vvug commented Dec 6, 2016

Got it. Indeed, what I expected was an interactive plot on plt.show(), without setting handle=True.

@krischer

This comment has been minimized.

Member

krischer commented Dec 6, 2016

I think it should be safe to just always return the handle. There is not downside except the return value in an interactive sessions. Should be an okay trade-off. Opinions?

@krischer krischer modified the milestones: 1.1.0, 1.1.1 Dec 6, 2016

@vvug

This comment has been minimized.

vvug commented Dec 6, 2016

The double plot would be quite annoying... I just see this.

@krischer

This comment has been minimized.

Member

krischer commented Dec 7, 2016

This issue is now a PR that causes the plot() method to almost always return the figure handle. The only downside as far as I can see is that plots might now appear twice in the notebook it its the last command in a cell. I'm not sure there is anything we can do against that.

@paride

This comment has been minimized.

Member

paride commented Dec 7, 2016

Hi @krischer. Can't we check if matplotlib.get_backend() is nbAgg, and return the handle only in this case? On nbagg:

http://matplotlib.org/users/whats_new.html#the-nbagg-backend

The idea is to avoid running plt.show() if the backend is nbAgg, preventing the double plot. Could it work?

@krischer

This comment has been minimized.

Member

krischer commented Dec 7, 2016

Unfortunately not - the jupyter notebooks would then only show the plot if st.plot() is the last statement in a cell - they always show (a potentially pretty) representation of the last return value if any.

@paride

This comment has been minimized.

Member

paride commented Dec 7, 2016

I see. The funny thing is that calling st.plot().show() does not result in a double plot. Is there way to disable the .show() auto-call? I'd have to call it explicitly every time, but the behavior would always be consistent, not depending on python/ipython/jupyer or on the position of the .plot() call in the cell.

@QuLogic

This comment has been minimized.

Member

QuLogic commented Dec 9, 2016

The funny thing is that calling st.plot().show() does not result in a double plot.

I assume you mean st.plot(handle=True).show()? And yes, that wouldn't plot double because .show() uses the figure and does not return the figure again.

Is there way to disable the .show() auto-call?

Passing handle=True already disables calling show(); these are unrelated.

@megies

This comment has been minimized.

Member

megies commented Mar 28, 2017

Rebased on current master and force-pushed for fresh CI.

@megies

This comment has been minimized.

Member

megies commented May 9, 2017

Rebased on current master again and force-pushed for fresh CI.

@megies megies modified the milestones: 1.1.1, 1.1.0 Jul 27, 2017

@megies

This comment has been minimized.

Member

megies commented Jul 27, 2017

Punting to 1.1.1.. otherwise we'll never even get to 1.1.0

Everybody feel free to tackle it of course..

@megies megies changed the base branch from master to maintenance_1.1.x Nov 1, 2017

@megies

This comment has been minimized.

Member

megies commented Nov 1, 2017

Changed base branch to maintenance_1.1.x. Please ideally rebase on this base branch, eventually.

@megies

This comment has been minimized.

Member

megies commented May 4, 2018

Rebased + force pushed

@megies

This comment has been minimized.

Member

megies commented May 7, 2018

Fixed the one doc test failure, rebased, force-pushed

@megies

megies approved these changes May 7, 2018

Works for me

@megies megies merged commit ecacfd7 into obspy:maintenance_1.1.x May 7, 2018

5 of 6 checks passed

docker-testbot docker testbot results not available yet
ci/circleci Your tests passed on CircleCI!
Details
codecov/patch 100% of diff hit (target 90%)
Details
codecov/project 88% (+1.07%) compared to 53edb29
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@megies megies deleted the krischer:always-return-handle branch May 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment