1

I'm implementing some codes using PyCharm Community Edition 2016.1.4 as environment. I have the following simple code:

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

If I simply run it I correctly obtain the proper image:

enter image description here

At the contrary, if I go in debug mode, when I arrive at the line:

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

I get this error:

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

Can you imagine why?

Andrea Ianni
  • 829
  • 12
  • 24
  • 2
    Possibly relevant: http://stackoverflow.com/questions/1736015/debugging-a-pyqt4-app . Basically, when debugging, you have to first get pyqt to stop its event loop. – Scott Mermelstein Nov 10 '16 at 17:31

1 Answers1

1

The python error was kinda misleading. The real problem was the lack (for some reasons due to a double installation of Python) of a Python binding: PyQt4.

Just go here and, choose the proper installer and... just run it! You can also avoid to close-and-reopen the pyCharm (in a couple of seconds it fixes itself and no more errors). enter image description here

Andrea Ianni
  • 829
  • 12
  • 24