matplotlib.scatter()在Python 3.6上不与Numpy一起使用

Tho*_*tto 9 python numpy matplotlib

我在理解为什么matplotlib.scatter()在使用Python 3.6.3作为解释器时不断抛出以下异常时遇到了一些困难,但在使用内置于我的MacBook的2.7时工作正常:

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
Run Code Online (Sandbox Code Playgroud)

我正在尝试执行以下代码:

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])
Run Code Online (Sandbox Code Playgroud)

我将该函数称为:

plot_boundary(p, X, T)
Run Code Online (Sandbox Code Playgroud)

X是[11x2] Numpy数组.

如果我将解释器设置为MacOS上的内置Python 2.7,则代码运行正常,将其设置为Python 3.6.2或3.6.3会导致上述错误.Matplotlib版本在前一种情况下为1.3.1,在后一种情况下为2.1.

有任何想法吗?

afl*_*ler 24

c需要一维数组.

T.ravel()应该做的伎俩.


Ahm*_*aza 5

plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)
Run Code Online (Sandbox Code Playgroud)

在此函数c中,需要一维数组,如以上答案所述,请使用T.ravel或T.reshape(400,)