Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib.scatter() not working with Numpy on Python 3.6

I'm having some difficulty in understanding why matplotlib.scatter() keeps throwing the following exception when using Python 3.6.3 as an interpreter but works fine when using 2.7 that is built-in to my MacBook:

Traceback (most recent call last):
  File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 132, in to_rgba
    rgba = _colors_full_map.cache[c, alpha]
TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4050, in scatter
    colors = mcolors.to_rgba_array(c)
  File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 233, in to_rgba_array
    result[i] = to_rgba(cc, alpha)
  File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 134, in to_rgba
    rgba = _to_rgba_no_colorcycle(c, alpha)
  File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/colors.py", line 189, in _to_rgba_no_colorcycle
    raise ValueError("RGBA sequence should have length 3 or 4")
ValueError: RGBA sequence should have length 3 or 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 458, in <module>
    main()
  File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 455, in main
    run_part1()
  File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 156, in run_part1
    plot_boundary(p, X, T)
  File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 142, in plot_boundary
    plot_data(X, targets)
  File "/Users/thomastiotto/Documents/USI/1 semester/Machine Learning/Assignments/Assignment 1/skeleton.py", line 129, in plot_data
    plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)
  File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3357, in scatter
    edgecolors=edgecolors, data=data, **kwargs)
  File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/__init__.py", line 1710, in inner
    return func(ax, *args, **kwargs)
  File "/Users/thomastiotto/python_envs/MachineLearning/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4055, in scatter
    raise ValueError(msg.format(c.shape, x.size, y.size))
ValueError: c of shape (11, 1) not acceptable as a color sequence for x with size 11, y with size 11

I am trying to execute the following code:

def plot_data(X, T):
    """
    Plots the 2D data as a scatterplot
    """
    plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)


def plot_boundary(model, X, targets, threshold=0.0):
    """
    Plots the data and the boundary lane which separates the input space into two classes.
    """
    x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
    y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
    xx, yy = np.meshgrid(np.linspace(x_min, x_max, 200), np.linspace(y_min, y_max, 200))
    X_grid = np.c_[xx.ravel(), yy.ravel()]
    y = model.forward(X_grid)
    plt.contourf(xx, yy, y.reshape(*xx.shape) < threshold, alpha=0.5)
    plot_data(X, targets)
    plt.ylim([y_min, y_max])
    plt.xlim([x_min, x_max])

I call the function as:

plot_boundary(p, X, T)

With X being an [11x2] Numpy array.

If I set my interpreter to the built-in Python 2.7 on MacOS the code runs fine, setting it to Python 3.6.2 or 3.6.3 results in the error above. Matplotlib version is 1.3.1 in former case and 2.1 in the latter.

Any ideas?

like image 397
Thomas Tiotto Avatar asked Oct 14 '17 16:10

Thomas Tiotto


People also ask

How do you plot a scatter 3D in python?

Generally 3D scatter plot is created by using ax. scatter3D() the function of the matplotlib library which accepts a data sets of X, Y and Z to create the plot while the rest of the attributes of the function are the same as that of two dimensional scatter plot.

What is CMAP in scatter plot?

cmap: A map of colors to use in the plot.

Which function call will create the scatter plot?

scatter() and plt. plot() You can also produce the scatter plot shown above using another function within matplotlib. pyplot .


1 Answers

c requires a single dimensional array.

T.ravel() should do the trick.

like image 126
aflaisler Avatar answered Sep 22 '22 07:09

aflaisler