Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compatibility error in pycharm using plt.show()

I want to plot the pca components graph according to the following code in pycharm.

import numpy as np
import matplotlib.pyplot as plt

from sklearn import linear_model, decomposition, datasets
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV

logistic = linear_model.LogisticRegression()

pca = decomposition.PCA()
pipe = Pipeline(steps = [('pca',pca), ('logistic', logistic)])

digits = datasets.load_digits()
x_digits = digits.data
y_digits = digits.target

# plot pca spectrum
pca.fit(x_digits)

plt.figure(1, figsize=(4,3))
# clear the current figure
plt.clf()
# add axes
plt.axes([.2,.2,.7,.7])
plt.plot(pca.explained_variance_, linewidth = 2)

plt.xlabel('n_components')
plt.ylabel('explained_variance_')

# prediction
n_comp = [20, 40, 64]
# logspace default is base 10, this is 10^-4 to 10^4
cs = np.logspace(-4, 4, 3)

# parameters of pipelines can be set using '__' separated parameter names:
estimator = GridSearchCV(pipe,
                         dict(pca__n_components = n_comp,
                              logistic__C = cs))
estimator.fit(x_digits, y_digits)

plt.axvline(estimator.best_estimator_.named_steps['pca'].n_components,
            linestyle = ':',label = 'n_compoenents chosen')
plt.legend(prop = dict(size = 12))
plt.axis('tight')
plt.show()

However I got a error message as :

UserWarning: This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.warnings.warn("This figure includes Axes that are not "

and

  File "C:\Program Files\JetBrains\PyCharm 2018.1.1\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 25, in __call__
    manager.show(**kwargs)
  File "C:\Program Files\JetBrains\PyCharm 2018.1.1\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 107, in show
    self.canvas.show()
  File "C:\Program Files\JetBrains\PyCharm 2018.1.1\helpers\pycharm_matplotlib_backend\backend_interagg.py", line 62, in show
    self.figure.tight_layout()
  File "C:\Users\User\Anaconda3\lib\site-packages\matplotlib\figure.py", line 1752, in tight_layout
    rect=rect)
  File "C:\Users\User\Anaconda3\lib\site-packages\matplotlib\tight_layout.py", line 322, in get_tight_layout_figure
    max_nrows = max(nrows_list)
  ValueError: max() arg is an empty sequence

And I tried the same code in spyder, it surprised to work.

What is wrong with the pycharm plot setting ? Both of spyder and pycharm are with python 3.5.

like image 549
exteral Avatar asked May 09 '26 20:05

exteral


1 Answers

Quick solution: disable the Python Scientific plot window in Pycharm(Then it will use the default matplotlib backend)

File > Settings > Tools >  Python > show plots in tool window
like image 158
Qing Yin Avatar answered May 11 '26 10:05

Qing Yin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!