Can't print the text on jupyter notebook using `matplotlib.pyplot`

前端 未结 1 1131
独厮守ぢ
独厮守ぢ 2020-12-12 08:34

Here is my sample code on jupyter notebook:

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))

def onclick(event):
    print(\'button=         


        
相关标签:
1条回答
  • 2020-12-12 09:04

    Jupyter by default shows png images of the plot. In order to get an interactive figure, you would need the %matplotlib notebook backend.

    The next problem is that it's currently not possible to interactively print to the cell output. This exact example is already subject of this GitHub issue. A solution may be expected for matplotlib version 2.1.

    As of now you might want to print the output to the figure canvas, e.g. as figure title.

    To give an example:

    import matplotlib.pyplot as plt
    import numpy as np
    %matplotlib notebook 
    
    fig = plt.figure();
    ax = fig.add_subplot(111)
    ax.plot(np.random.rand(10))
    
    def onclick(event):
        a = ('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
              (event.button, event.x, event.y, event.xdata, event.ydata))
        ax.set_title(a)
    
    cid = fig.canvas.mpl_connect('button_press_event', onclick)
    
    plt.show()
    

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