值错误:x和y必须具有相同的第一个维度

时间:2016-09-22 17:08:56

标签: python numpy matplotlib

让我先简要介绍一下,我正在处理一个包含5400个数据点的.txt文件。每个是24小时内的平均16秒(24小时* 3600秒/小时= 86400 ... 86400/16 = 5400)。简而言之,这是高级成分实验卫星的入站粒子场屈服的z方向上的平均磁场强度。公开数据here。所以当我试图绘制它时说错误

Value Error: x and y must have the same first dimension

所以我创造了一个由16个单位分开的5400点的numpy lin空间。我这样做是因为我认为我的尺寸与我之前定义的数组不匹配。但是现在我确信这两个数组具有相同的维度,但它仍然会返回该值错误。代码如下:

首先尝试(没有linspace):

import numpy as np
import matplotlib as plt

Bz = np.loadtxt(r"C:\Users\Schmidt\Desktop\Project\Data\ACE\MAG\ACE_MAG_Data_20151202_GSM.txt", dtype = bytes).astype(float)

Start_ACE = dt.date(2015,12,2)
Finish_ACE = dt.date(2015,12,2)

dt_Mag = 16

time_Mag = np.arange(Start_ACE, Finish_ACE, dt_Mag)

plt.subplot(3,1,1)
plt.plot(time_Mag, Bz)
plt.title('Bz 2015 12 02')

第二次尝试(使用linspace):

import numpy as np
import matplotlib as plt

Bz = np.loadtxt(r"C:\Users\Schmidt\Desktop\Project\Data\ACE\MAG\ACE_MAG_Data_20151202_GSM.txt", dtype = bytes).astype(float)

Mag_time = np.linspace(0,5399,16, dtype = float)

plt.subplot(3,1,1)
plt.plot(Mag_time, Bz)
plt.title('Bz 2015 12 02')

除了它是一个尺寸问题,我不知道还有什么可以阻止这个绘图程序。

完整追溯:

ValueError                                Traceback (most recent call last)
<ipython-input-68-c5dc0bdf5117> in <module>()
      1 plt.subplot(3,1,1)
----> 2 plt.plot(Mag_time, Bz)
      3 plt.title('Bz 2015 12 02')

C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(*args, **kwargs)
   3152         ax.hold(hold)
   3153     try:
-> 3154         ret = ax.plot(*args, **kwargs)
   3155     finally:
   3156         ax.hold(washold)

C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\__init__.py in inner(ax, *args, **kwargs)
   1809                     warnings.warn(msg % (label_namer, func.__name__),
   1810                                   RuntimeWarning, stacklevel=2)
-> 1811             return func(ax, *args, **kwargs)
   1812         pre_doc = inner.__doc__
   1813         if pre_doc is None:

C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, *args, **kwargs)
   1422             kwargs['color'] = c
   1423 
-> 1424         for line in self._get_lines(*args, **kwargs):
   1425             self.add_line(line)
   1426             lines.append(line)

C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _grab_next_args(self, *args, **kwargs)
    384                 return
    385             if len(remaining) <= 3:
--> 386                 for seg in self._plot_args(remaining, kwargs):
    387                     yield seg
    388                 return

C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
    362             x, y = index_of(tup[-1])
    363 
--> 364         x, y = self._xy_from_xy(x, y)
    365 
    366         if self.command == 'plot':

C:\Users\Schmidt\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in _xy_from_xy(self, x, y)
    221         y = _check_1d(y)
    222         if x.shape[0] != y.shape[0]:
--> 223             raise ValueError("x and y must have same first dimension")
    224         if x.ndim > 2 or y.ndim > 2:
    225             raise ValueError("x and y can be no greater than 2-D")

ValueError: x and y must have same first dimension

1 个答案:

答案 0 :(得分:0)

问题在于选择数组创建。我应该使用范围而不是linspace。

Mag_time = np.arange(0,86400, 16, dtype = float)