ipython notebook on linux VM running matplotlib interactive with nbagg

后端 未结 1 1007
离开以前
离开以前 2020-12-06 18:58

I want buttons and other interactive matplotlib objects to appear from within my ipython notebook. \"screenshot\"

1条回答
  •  我在风中等你
    2020-12-06 19:39

    Basically you are facing two issues

    • the %pylab inlinecall overrides the matplotlib.use('nbagg')call, to use the inline backend instead of the nbagg backend which you are actually wanting. If you use a recent version of IPython (2.3) you can directly use %matplotlib nbagg (or %matplotlib notebook) to load the nbagg backend instead of your %pylabcall.

    • once you enabled the nbagg backend you will need to explicitly show it, ie. add a plt.show() call at the end of your script -> Update: with IPython 2.3.1 this is no longer needed (thanks @tcaswell for the hint)

    With this you get the interactive matplotlib experience embedded in the IPython notebook. However, a quick try of your code does't yield to the desired result. The Button reacts and the callback is executed but the print call doesn't show anything. Anyway, to see that it's working try the following simple example (requires IPython 2.3):

    %matplotlib nbagg
    from matplotlib.widgets import Button
    import matplotlib.pyplot as plt
    def callback(event):
        plt.text(event.xdata, event.ydata, 'clicked')
    
    f,a = plt.subplots(1)
    b1 = Button(a,'Button1')
    b1.on_clicked(callback)
    plt.show()
    

    Btw. it is highly recommended to use %matplotlib instead of %pylab as later leads to some side effects, see here.

    0 讨论(0)
提交回复
热议问题