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

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

问题描述

这是我在 jupyter notebook 上的示例代码:

fig = plt.figure()ax = fig.add_subplot(111)ax.plot(np.random.rand(10))定义点击(事件):打印('按钮=%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 后端.

下一个问题是目前无法交互式打印到单元格输出.这个确切的例子已经是

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

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

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