首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在jupyter笔记本内使用tkinter进行nltk绘图

在jupyter笔记本内使用tkinter进行nltk绘图
EN

Stack Overflow用户
提问于 2017-07-03 07:51:41
回答 2查看 4.6K关注 0票数 2

我正在试图在内部绘制nltk的图形(内嵌)。但有错误:

代码语言:javascript
复制
TclError: no display name and no $DISPLAY environment variable

我尝试将$DISPLAY设置为不同的值:

代码语言:javascript
复制
$env DISPLAY=0.0
# or
$env DISPLAY=inline
# or
$env DISPLAY=

但是有错误(或类似的):

代码语言:javascript
复制
TclError: couldn't connect to display "0.0"

这是我的代码https://github.com/hyzhak/nltk-experiments/blob/master/main.ipynb,最后一个单元格。

环境:正式anaconda3码头-- continuumio/anaconda3:4.4.0 https://github.com/ContinuumIO/docker-imagesnltk==3.2.3在里面。

代码语言:javascript
复制
Python 3.6.1 |Anaconda 4.4.0 (64-bit)| (default, May 11 2017, 13:09:58) 
Type "copyright", "credits" or "license" for more information.

IPython 5.3.0 -- An enhanced Interactive Python.

如何解决这个错误和内联 nltk图在jupyter notebook中的问题?

更新1

trees根据nltk树的来源绘制它使用tkinter

更新2

我在官方的nltk github存储库https://github.com/nltk/nltk/issues/1765中也问过同样的问题。

更新3

我认为错误的原因可能是无头主机(码头)。我已经安装了xvfb,但似乎已经足够了。

代码语言:javascript
复制
RUN apt-get install -y xvfb

解决方案

我认为xvfb是默认启动的,但应该显式运行。所以,在我启动它之后,我可以制作nltk树形图的截图。

EN

回答 2

Stack Overflow用户

发布于 2017-07-03 23:33:30

代码语言:javascript
复制
import os
import nltk
from IPython.display import Image

chunkGram = r"""Chunk: {<RB.?>*<VB.?>*<NNP>+<NN>?}"""
chunkParser = nltk.RegexpParser(chunkGram)

tagged = [('Tonight', 'NN'), ('we', 'PRP'), ('are', 'VBP'), ('comforted', 'VBN'), ('by', 'IN'), ('the', 'DT'), ('hope', 'NN'), ('of', 'IN'), ('a', 'DT'), ('glad', 'JJ'), ('reunion', 'NN'), ('with', 'IN'), ('the', 'DT'), ('husband', 'NN'), ('who', 'WP'), ('was', 'VBD'), ('taken', 'VBN'), ('so', 'RB'), ('long', 'RB'), ('ago', 'RB'), (',', ','), ('and', 'CC'), ('we', 'PRP'), ('are', 'VBP'), ('grateful', 'JJ'), ('for', 'IN'), ('the', 'DT'), ('good', 'JJ'), ('life', 'NN'), ('of', 'IN'), ('Coretta', 'NNP'), ('Scott', 'NNP'), ('King', 'NNP'), ('.', '.')]
chunked = chunkParser.parse(tagged)
nltk.draw.tree.TreeView(chunked)._cframe.print_to_file('output.ps')
os.system('convert output.ps output.png')

Image(filename='output.png') 

输出

票数 1
EN

Stack Overflow用户

发布于 2017-07-03 08:19:51

尝试将以下代码片段合并到代码中

代码语言:javascript
复制
import os
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
    print('no display found. Using non-interactive Agg backend')
    mpl.use('Agg')
import matplotlib.pyplot as plt

不管怎样。您的问题是Matplotlib对x使用后端的默认调用显然会导致问题。这个答案绝不是唯一可能的解决办法,但我认为这将解决你的问题。请记住,在导入pyplot之前切换后端是很重要的。

或者,您可以尝试IPython.core.display.display(分块)

确保您的笔记本服务器启动时设置了-X标志。

代码语言:javascript
复制
def jupyter_draw_nltk_tree(tree):
    from IPython.display import Image, display
    from nltk.draw import TreeWidget
    from nltk.draw.util import CanvasFrame
    import subprocess
    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), tree)
    tc['node_font'] = 'arial 13 bold'
    tc['leaf_font'] = 'arial 14'
    tc['node_color'] = '#005990'
    tc['leaf_color'] = '#3F8F57'
    tc['line_color'] = '#175252'
    cf.add_widget(tc, 10, 10)
    cf.print_to_file('tmp_tree_output.ps')
    cf.destroy()

    args = (['convert', 'tmp_tree_output.ps', 'tmp_tree_output.png'])
    subprocess.call(args)
    display(Image(filename='tmp_tree_output.png'))
    os.system('rm tmp_tree_output.ps tmp_tree_output.png')

jupyter_draw_nltk_tree(chunked)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44880337

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档