简体   繁体   English

使用matplotlib和numpy绘制整个数组时出错

[英]Error when plotting whole array using matplotlib and numpy

I am trying to plot a scatter chart using numpy and matplotlib. 我正在尝试使用numpy和matplotlib绘制散点图。 It is very simple, I have 2 data files, each contains single column set of data. 这是很简单的,我有2个数据文件,每个文件包含单个列的数据集。 Both files have same number of data (I've checked this over and over again to make sure). 这两个文件具有相同数量的数据(我已经一遍又一遍地检查以确保内容)。

This is what I've done: 这是我所做的:

import numpy as np
import pylab as pl

xdata = np.loadtxt('data.txt')
ydata = np.loadtxt('data1.txt')

pl.plot(xdata, ydata, 'ro')

pl.show()

and it gives me this error 它给我这个错误

File "C:/1aProjects/Python_Aryo/Plotting/test_plot.py", line 10, in <module>
pl.plot(xdata, ydata, 'ro')
File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 2987, in plot
ret = ax.plot(*args, **kwargs)
File "C:\Python34\lib\site-packages\matplotlib\axes.py", line 4144, in plot
for line in self._get_lines(*args, **kwargs):
File "C:\Python34\lib\site-packages\matplotlib\axes.py", line 319, in _grab_next_args
for seg in self._plot_args(remaining, kwargs):
File "C:\Python34\lib\site-packages\matplotlib\axes.py", line 297, in _plot_args
x, y = self._xy_from_xy(x, y)
File "C:\Python34\lib\site-packages\matplotlib\axes.py", line 239, in _xy_from_xy
raise ValueError("x and y must have same first dimension")
ValueError: x and y must have same first dimension

it says the data does not have the same dimension. 它说,数据不具有相同的尺寸。

Then I tried to plot only in certain range, and it works but until row no 72402 as shown below. 然后我试图绘制只在一定的范围内,和它的作品,但直到行没有72402,如下图所示。

pl.plot(xdata[0:72402], ydata[0:72402], 'ro')

if I put anything more than 72402, it gives me the same error msg. 如果我把任何超过72402,它给了我同样的错误味精。 Telling that the array does not have the same size. 告诉该阵列不具有相同的尺寸。 My data actually has up row 72413!, it is just 11 more rows! 我的数据实际上有上一行72413 !,仅多出11行! it is quite annoying isn't it? 这很烦人吗?

Can anyone help? 有人可以帮忙吗?

It is attempting to plot x and y variables, as in a scatter plot. 如散点图所示,它正在尝试绘制x和y变量。 The two vectors need to have the same length, which is why you see that you are able to plot up to the smallest of the two lengths. 两个向量必须具有相同的长度,这就是为什么看到最大能够绘制两个长度中最小的一个的原因。 There's no getting around it. 没有解决的办法。 The length of the two arrays is clearly not equal. 两个数组的长度显然不相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM