无法使用`matplotlib.pyplot`在jupyter笔记本上打印文本 [英] Can't print the text on jupyter notebook using `matplotlib.pyplot`

查看:107
本文介绍了无法使用`matplotlib.pyplot`在jupyter笔记本上打印文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在jupyter笔记本上的示例代码:

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默认显示图的png图像.为了获得交互式图形,您需要%matplotlib notebook后端.

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

下一个问题是,当前无法交互式打印到单元格输出.此确切示例已经是此GitHub问题的主题. matplotlib 2.1版可能需要解决方案.

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.

举个例子:

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

这篇关于无法使用`matplotlib.pyplot`在jupyter笔记本上打印文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆