IPython Notebook widgets for Matplotlib interactivity

后端 未结 3 700
悲&欢浪女
悲&欢浪女 2021-02-06 00:05

I would like to use the ipython notebook widgets to add some degree of interactivity to inline matplotlib plots.

In general the plot can be quite heavy and I want to on

3条回答
  •  暖寄归人
    2021-02-06 00:33

    You can do this in a very strait forward way using the new(ish) notebook backend

    %matplotlib notebook
    import matplotlib.pyplot as plt
    from IPython.html.widgets import interactive
    
    fig, ax = plt.subplots()
    ax.plot(range(5))
    
    
    vline = ax.axvline(1, color='k')
    hline = ax.axhline(0.5, color='k')
    
    def set_cursor(x, y):
        vline.set_xdata((x, x))
        hline.set_ydata((y, y))
        ax.figure.canvas.draw_idle()
    

    and in a separate cell:

    interactive(set_cursor, x=ax.get_xlim(), y=ax.get_ylim())
    

    This will still re-draw the entire figure every time you move the cursor because notebook does not currently support blitting (which is being worked on https://github.com/matplotlib/matplotlib/pull/4290 )

提交回复
热议问题