Runtimeexception when using colorbar() Spyder IDE

Asked

Viewed 93 times

2

I am a code to show the use of self-organizing maps and error appears when using colorbar function.

Error happens on line colorbar(). Before that the way out was:

Saida

So when I run the line colorbar(). The following error appears:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-14-dffe8fcb3a04> in <module>
----> 1 colorbar()

~/anaconda3/lib/python3.7/site-packages/matplotlib/pyplot.py in colorbar(mappable, cax, ax, **kw)
   2091         mappable = gci()
   2092         if mappable is None:
-> 2093             raise RuntimeError('No mappable was found to use for colorbar '
   2094                                'creation. First define a mappable such as '
   2095                                'an image (with imshow) or a contour set ('

RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).
# Self Organizing Map

# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

# Importing the dataset
dataset = pd.read_csv('Credit_Card_Applications.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

# Feature Scaling
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0, 1))
X = sc.fit_transform(X)

# Training the SOM
from minisom import MiniSom
som = MiniSom(x = 10, y = 10, input_len = 15, sigma = 1.0, learning_rate = 0.5)
som.random_weights_init(X)
som.train_random(data = X, num_iteration = 100)

# Visualizing the results
from pylab import bone, pcolor, colorbar, plot, show
bone()
pcolor(som.distance_map().T)
colorbar()
markers = ['o', 's']
colors = ['r', 'g']
for i, x in enumerate(X):
    w = som.winner(x)
    plot(w[0] + 0.5,
         w[1] + 0.5,
         markers[y[i]],
         markeredgecolor = colors[y[i]],
         markerfacecolor = 'None',
         markersize = 10,
         markeredgewidth = 2)
show()

2 answers

0


I was using the Jupyter Notebook, and I was using separate instructions to do the map-showing part. The problem disappeared when I executed all this piece at once, instead of fragmenting the execution of it.

from pylab import bone, pcolor, colorbar, plot, show

# Initializing the figure that contains the map
bone()
pcolor(som.distance_map().T)
colorbar()
markers = ['o', 's'] # circles and squares
colors = ['r', 'g'] # red and green

# i for lines and X for vector that represents the customer
for i, x in enumerate(X):
    w = som.winner(x) # the winning node of customer x
    plot(w[0] + 0.5, 
         w[1] + 0.5,
         markers[Y[i]],
         markeredgecolor = colors[Y[i]],
         markerfacecolor = 'None',
         markersize=10,
         markeredgewidth=2) # +0.5 to plot on the center of square on the map

show()

0

I suspect there’s a call to show shortly after pcolor for:

  • A graph is displayed
  • The function show "consumes" the graphics made previously - and therefore it is natural that the call to colorbar fail, as there are no more charts to apply the bar chart
  • The following code snippet also causes the same exception that occurred to you:
from pylab import bone, pcolor, colorbar, show
import matplotlib.pyplot as plt
import numpy as np

dados = np.clip(np.random.randn(20, 20), -1, 1)
bone()
pcolor(dados)
show()
colorbar()
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-9f16f12cc15b> in <module>()
      7 pcolor(dados)
      8 show()
----> 9 colorbar()

/usr/lib/python3.6/site-packages/matplotlib/pyplot.py in colorbar(mappable, cax, ax, **kw)
   2319         mappable = gci()
   2320         if mappable is None:
-> 2321             raise RuntimeError('No mappable was found to use for colorbar '
   2322                                'creation. First define a mappable such as '
   2323                                'an image (with imshow) or a contour set ('

RuntimeError: No mappable was found to use for colorbar creation. First define a mappable such as an image (with imshow) or a contour set (with contourf).

If that’s the case, I see some possibilities:

  • During development, you temporarily added a call to show after the call to pcolor to test the progress of the program? If yes, just remove it - in other words, the code you shared, with only one show, should be working by now.
  • Your IDE can make implicit call to the function show. To check, you can save the code you made into a file .py, and run directly via console.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.