简体   繁体   中英

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

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=%d, x=%d, y=%d, xdata=%f, ydata=%f' %
          (event.button, event.x, event.y, event.xdata, event.ydata))

cid = fig.canvas.mpl_connect('button_press_event', onclick)

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, eg 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()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM