tkinter、python和seaborn的问题:\u tkinter.TclError:没有显示名称和$display环境变量

tkinter、python和seaborn的问题:\u tkinter.TclError:没有显示名称和$display环境变量,python,python-2.7,matplotlib,tkinter,seaborn,Python,Python 2.7,Matplotlib,Tkinter,Seaborn,故事 pandas、numpy、seaborn和matplotlib在系统上安装时没有错误。我从 片段 from string import letters import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt sns.set(style="white") # Generate a large random dataset rs = np.random.Rand

故事

pandas、numpy、seaborn和matplotlib在系统上安装时没有错误。我从

片段

from string import letters
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="white")

# Generate a large random dataset
rs = np.random.RandomState(33)
d = pd.DataFrame(data=rs.normal(size=(100, 26)),
                 columns=list(letters[:26]))

# Compute the correlation matrix
corr = d.corr()

# Generate a mask for the upper triangle
mask = np.zeros_like(corr, dtype=np.bool)
mask[np.triu_indices_from(mask)] = True

# Set up the matplotlib figure
f, ax = plt.subplots(figsize=(11, 9))

# Generate a custom diverging colormap
cmap = sns.diverging_palette(220, 10, as_cmap=True)

# Draw the heatmap with the mask and correct aspect ratio
sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3,
            square=True, xticklabels=5, yticklabels=5,
            linewidths=.5, cbar_kws={"shrink": .5}, ax=ax)
它会导致以下错误:

Traceback (most recent call last):
  File "seaborn-example.py", line 4, in <module>
    import seaborn as sns
  File "/usr/local/lib/python2.7/site-packages/seaborn/__init__.py", line 6, in <module>
    from .rcmod import *
  File "/usr/local/lib/python2.7/site-packages/seaborn/rcmod.py", line 8, in <module>
    from . import palettes, _orig_rc_params
  File "/usr/local/lib/python2.7/site-packages/seaborn/palettes.py", line 12, in <module>
    from .utils import desaturate, set_hls_values, get_color_cycle
  File "/usr/local/lib/python2.7/site-packages/seaborn/utils.py", line 12, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/lib/python2.7/site-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/usr/local/lib/python2.7/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python2.7/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
然后,我按照这个方法来重建Python

https://github.com/h2oai/h2o-2/wiki/installing-python-2.7-on-centos-6.3.-follow-this-sequence-exactly-for-centos-machine-only
当我尝试重建时得到了这个-tkinter仍然存在一些问题:

  Traceback (most recent call last):
  File "seaborn-example.py", line 22, in <module>
    f, ax = plt.subplots(figsize=(11, 9))
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 1203, in subplots
    fig = figure(**fig_kw)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/pyplot.py", line 535, in figure
    **kwargs)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 81, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 89, in new_figure_manager_given_figure
    window = Tk.Tk()
  File "/usr/local/lib/python2.7/lib-tk/Tkinter.py", line 1745, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
回溯(最近一次呼叫最后一次):
文件“seaborn example.py”,第22行,在
f、 ax=plt.子批次(图尺寸=(11,9))
文件“/usr/local/lib/python2.7/site packages/matplotlib/pyplot.py”,第1203行,在子批中
图=图(**图千瓦)
文件“/usr/local/lib/python2.7/site packages/matplotlib/pyplot.py”,图中第535行
**kwargs)
文件“/usr/local/lib/python2.7/site packages/matplotlib/backends/backend\u tkagg.py”,第81行,在新的图形管理器中
返回新的\u图形\u管理器\u给定的\u图形(num,figure)
文件“/usr/local/lib/python2.7/site packages/matplotlib/backends/backend\u tkagg.py”,第89行,在新图\u manager\u给定图\u中
window=Tk.Tk()
文件“/usr/local/lib/python2.7/lib-tk/Tkinter.py”,第1745行,在__
self.tk=\u tkinter.create(屏幕名、基本名、类名、交互、wantobjects、useTk、同步、使用)
_tkinter.TclError:没有显示名称和$display环境变量

这里怎么了?

在文件的开头

import matplotlib
matplotlib.use('Agg')

在文件的开头

import matplotlib
matplotlib.use('Agg')

您是否有与运行该程序的进程相关联的图形显示(即:您是否已登录,并且可以使用鼠标查看窗口)?@BryanOakley-我明白您的意思。这个问题与我有关。这里有我的答案。你是否有一个与运行程序的进程相关联的图形显示(即:你是否已登录,并且可以使用鼠标查看窗口)?@BryanOakley-我明白你的意思。这个问题与我有关。这里有我的答案。