【问题标题】:matplotlib.scatter() not working with Numpy on Python 3.6matplotlib.scatter() 不能在 Python 3.6 上使用 Numpy
【发布时间】:2017-10-14 16:33:33
【问题描述】:

我很难理解为什么 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

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

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

我把函数称为:

plot_boundary(p, X, T)

X 是一个 [11x2] Numpy 数组。

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

有什么想法吗?

【问题讨论】:

  • 11x1矩阵和11向量是有区别的。使用c=T[:, 0]
  • 完美!不知道为什么它在旧版本的软件包中有效,而不是在最新版本中..
  • 我在 Python 2.6.2 和 Matplotlib 2.1 中遇到了完全相同的问题

标签: python numpy matplotlib


【解决方案1】:

c 需要一维数组。

T.ravel() 应该可以解决问题。

【讨论】:

    【解决方案2】:
    plt.scatter(X[:, 0], X[:, 1], s=40, c=T, cmap=plt.cm.Spectral)
    

    在这个函数中,c 需要一维数组,如上面答案中提到的,使用T.ravelT.reshape(400,)

    【讨论】:

      【解决方案3】:

      您也可以使用c=np.squeeze(T)

      我认为这里的问题实际上是更大的 python/numpy 问题的一部分——它无法推断一维数组的正确用法。这浪费了大量时间编码和调试。

      【讨论】:

        【解决方案4】:

        使用 np.reshape

        import numpy as np
        
        t1 = np.array([[1,2,3,4,5,6] , [7,8,9,10,11,12]])
        t1_single = np.reshape(t1, -1)
        print(t1.shape)
        print(t1_single.shape)
        

        输出
        (2, 6)
        (12,)

        【讨论】:

          猜你喜欢
          • 2017-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-08-17
          • 2017-04-15
          • 1970-01-01
          相关资源
          最近更新 更多