gpt4 book ai didi

python - _tkinter.TclError : no display name and no $DISPLAY environment variable for new OS X Mavericks

转载 作者:行者123 更新时间:2023-11-28 16:39:58 26 4
gpt4 key购买 nike

这是我的第一篇文章,所以请原谅。

我正在尝试使用 matplotlib 创建动画,直到几天前我才能完美地完成这项工作,直到我升级到 OS X Mavericks,一切都变得一团糟。请注意,以下代码在 OS X Lion 中有效。

我在 Mavericks 中尝试运行这段代码时突然收到一条错误消息:

import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import pandas as pd
import mpl_toolkits.mplot3d.axes3d as p3

#----->READ AND FORMAT DATA-----------------------------------------------#
.
.
.
.

#-------------------------------------------------------------------------#

# Set up figure & 3D axis for animation
fig = plt.figure(figsize=(10,10))
ax = fig.add_axes([0, 0, 1, 1], projection='3d') # Correct way to make 3D plots

# set up lines and points
lines = sum([ax.plot([], [], [], '-')], [])
pts = sum([ax.plot([], [], [], 'o')], [])

# Setting the axes properties
ax.set_xlim3d([-2000, 2000])
ax.set_xlabel('X')
ax.set_ylim3d([1350, 1400])
ax.set_ylabel('Y')
ax.set_zlim3d([-340, 600])
ax.set_zlabel('Z')

# set point-of-view: specified by (altitude degrees, azimuth degrees)
ax.view_init(40, 0)
ax.set_title('animation')
ax.grid() # add grid

# Initialisation function: plot the background of each frame
def init():
for line, pt in zip(lines, pts):
# Lines
line.set_data([], [])
line.set_3d_properties([])
# Points
pt.set_data([], [])
pt.set_3d_properties([])
return lines + pts

# Animation function. This will be called sequentially with the frame number
def animate(i):
# we'll step two time-steps per frame. This leads to nice results.
i = (2.5 * i)

for line, pt, dat in zip(lines, pts, data):
x, y, z = dat[:i].T
# Lines
line.set_data(x, y)
line.set_3d_properties(z)
# Points
pt.set_data(x[-1:], y[-1:])
pt.set_3d_properties(z[-1:])

ax.view_init(45, i)
fig.canvas.draw()
return lines + pts

#----->ANIMATION-----------------------------------------------------------#

# Creating the Animation object
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=120, interval=30, blit=True)
# Save as mp4. This requires mplayer or ffmpeg to be installed
# anim.save('one_marker_3D.mp4', fps=15, extra_args=['-vcodec', 'libx264'])

plt.show()

这会产生以下错误消息:

Traceback (most recent call last):
File "shoulder_animation_walking_straight_3D.py", line 26, in <module>
fig = plt.figure(figsize=(10,10))
File "/Users/me/anaconda/lib/python2.7/site-packages/matplotlib/pyplot.py", line 423, in figure
**kwargs)
File "/Users/me/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 79, in new_figure_manager
return new_figure_manager_given_figure(num, figure)
File "/Users/me/anaconda/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 87, in new_figure_manager_given_figure
window = Tk.Tk()
File "/Users/me/anaconda/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

现在如您所见,我已尝试使用 TkAgg 后端而不是文档 (https://wiki.python.org/moin/TkInter) 推荐的“Agg”后端。这似乎会导致新版本的 OS X 出现问题,因为不再支持 X11,必须安装 XQuartz。这个我也做过。但它仍然给我同样的错误。

我还尝试使用 macports 安装 py33-tkinter,但这也不起作用,但也许我没有正确安装。我只是做了

sudo port py33-tkinter

总的来说,我对 python 和 unix 还是比较陌生。

更新(22/12/13):

我做了以下

Python 2.7.5 |Anaconda 1.8.0 (x86_64)| (default, Oct 24 2013, 07:02:20) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import Tkinter

然后运行 ​​Tkinter 测试

>>> Tkinter._test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/me/anaconda/lib/python2.7/lib-tk/Tkinter.py", line 3770, in _test
root = Tk()
File "/Users/me/anaconda/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
>>>

可能对比我更了解这一点的人有帮助。

最佳答案

解决方案

这是通过执行以下操作解决的,这是由 XQuartz 的问题引起的;

launchctl load -w /Library/LaunchAgents/org.macosforge.xquartz.startx.plist

我认为重新安装 XQuartz 会重新启用 launchd,但显然这并没有发生。现在一切正常。

关于python - _tkinter.TclError : no display name and no $DISPLAY environment variable for new OS X Mavericks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20725694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com