gpt4 book ai didi

Python:错误的调试

转载 作者:太空宇宙 更新时间:2023-11-03 15:58:44 37 4
gpt4 key购买 nike

我正在使用 PyCharm Community Edition 2016.1.4 作为环境来实现一些代码。我有以下简单代码:

print(__doc__)
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

from sklearn.cluster import KMeans
from sklearn import datasets

np.random.seed(5)

centers = [[1, 1], [-1, -1], [1, -1]]
iris = datasets.load_iris()
X = iris.data
y = iris.target

estimators = {'k_means_iris_3': KMeans(n_clusters=3),
'k_means_iris_8': KMeans(n_clusters=8),
'k_means_iris_bad_init': KMeans(n_clusters=3, n_init=1,
init='random')}

fignum = 1
name = 'k_means_iris_3'
est = KMeans(n_clusters=3)

fig = plt.figure(fignum, figsize=(4, 3))
plt.clf()
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

est.fit(X)
labels = est.labels_

ax.scatter(X[:, 3], X[:, 0], X[:, 2], c=labels.astype(np.float))

ax.w_xaxis.set_ticklabels([])
ax.w_yaxis.set_ticklabels([])
ax.w_zaxis.set_ticklabels([])
ax.set_xlabel('Petal width')
ax.set_ylabel('Sepal length')
ax.set_zlabel('Petal length')
fignum = fignum + 1

plt.show()

如果我只是运行它,我就会正确地获得正确的图像:

enter image description here

相反,如果我进入 Debug模式,当我到达该行时:

fig = plt.figure(fignum, figsize=(4, 3))

我收到此错误:

Traceback (most recent call last):
File "C:\Program Files\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-1-36b230119a6b>", line 1, in <module>
fig = plt.figure(fignum, figsize=(4, 3))
File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 535, in figure
**kwargs)
File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 44, in new_figure_manager
return new_figure_manager_given_figure(num, thisFig)
File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 51, in new_figure_manager_given_figure
canvas = FigureCanvasQTAgg(figure)
File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 223, in __init__
super(FigureCanvasQTAgg, self).__init__(figure=figure)
File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5agg.py", line 66, in __init__
super(FigureCanvasQTAggBase, self).__init__(figure=figure)
File "C:\Program Files\Anaconda\lib\site-packages\matplotlib\backends\backend_qt5.py", line 239, in __init__
super(FigureCanvasQT, self).__init__(figure=figure)
AttributeError: 'figure()' is not a Qt property or a signal

你能想象为什么吗?

最佳答案

Python 错误有点误导。真正的问题是缺少 Python 绑定(bind)(由于某些原因,由于 Python 的双重安装):PyQt4。

走吧here并且,选择正确的安装程序并...只需运行它!您还可以避免关闭并重新打开 pyCharm(几秒钟后它会自行修复并且不再出现错误)。 enter image description here

关于Python:错误的调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40533605/

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