matplotlib 散点数组长度不一样 [英] matplotlib scatter array lengths are not same

查看:99
本文介绍了matplotlib 散点数组长度不一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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',线宽= 3)plt.xticks(())plt.yticks(())plt.show()

它给了我这个错误

  Traceback(最近一次通话最近):< module>中的文件"C:\ wamp \ www \ python \ test1.py",第84行plt.scatter(x_test, y_test, color='black')文件C:\Python34\lib\site-packages\matplotlib\pyplot.py",第 3251 行,散点图edgecolors=edgecolors, data=data, **kwargs)内部的文件"C:\ Python34 \ lib \ site-packages \ matplotlib \ __ init__.py",第1812行返回 func(ax, *args, **kwargs)文件C:\Python34\lib\site-packages\matplotlib\axes\_axes.py",第 3840 行,散点图引发ValueError("x和y必须具有相同的大小")ValueError:x和y的大小必须相同

两个数组的长度都一样,我不明白这里是什么问题.请帮我谢谢

这是我的 y_predict 数组

y_predict = [ 91.76428571 103.1 86.85714286 401.44642857 339.69642857196.83571429 126.38928571 31.41071429 211.67857143 167.35357143288.53214286 107.36785714]

解决方案

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

print(x_test.shape)打印(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',线宽= 3)

i have 2 arrays like this

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]

i want these array to show scatter using matplotlib.

this is my code

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

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

plt.show() 

and it gives me this error

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

both array length are same and i don't understand what is the problem here. please help me thanks

this is my y_predict array

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]

解决方案

The x_test, like you provided it, doesn't make sense for a scatter plot. And the arrays are actually not the same size:

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

(12, 2)

(12,)

What you might want to have instead is something like this (using only the second column of x_test):

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

这篇关于matplotlib 散点数组长度不一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆