【问题标题】:matplotlib scatter array lengths are not samematplotlib 散点数组长度不相同
【发布时间】:2016-10-13 13:00:49
【问题描述】:

我有 2 个这样的数组

x_test = [[ 14.   1.] [ 14.   2.] [ 14.   3.] [ 14.   4.] [ 14.   5.] [ 14.   6.] [ 14.   7.] [ 14.   8.] [ 14.   9.] [ 14.  10.] [ 14.  11.] [ 14.  12.]]

y_test = [ 254.7  255.4  476.5   19.5   85.6  352.   238.7  144.8   83.5  278.8   449.6  312.7]

我希望这些数组使用 matplotlib 显示散点图。

这是我的代码

plt.scatter(x_test, y_test,  color='black')
plt.plot(x_test, y_predict, color='blue', linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show() 

它给了我这个错误

Traceback (most recent call last):
  File "C:\wamp\www\python\test1.py", line 84, in <module>
    plt.scatter(x_test, y_test,  color='black')
  File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 3251, in scatter
    edgecolors=edgecolors, data=data, **kwargs)
  File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 1812, in inner
    return func(ax, *args, **kwargs)
  File "C:\Python34\lib\site-packages\matplotlib\axes\_axes.py", line 3840, in scatter
    raise ValueError("x and y must be the same size")
ValueError: x and y must be the same size

两个数组长度相同,我不明白这里有什么问题。请帮帮我谢谢

这是我的 y_predict 数组

y_predict = [  91.76428571  103.1          86.85714286  401.44642857  339.69642857
  196.83571429  126.38928571   31.41071429  211.67857143  167.35357143
  288.53214286  107.36785714]

【问题讨论】:

    标签: python arrays matplotlib


    【解决方案1】:

    x_test,就像您提供的那样,对于散点图没有意义。而且数组实际上大小不一样:

    print(x_test.shape)
    print(y_test.shape)
    

    (12, 2)

    (12,)

    您可能想要的是这样的(仅使用x_test 的第二列):

    plt.scatter(x_test[:,1], y_test,  color='black')
    plt.plot(x_test[:,1], y_predict, color='blue', linewidth=3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-18
      • 2015-08-24
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多