python中的数组组合

2024-04-29 12:07:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试将两个一维数组中的值组合起来,形成类似[(a,b),(c,d),(e,f)…]的值。然而,当我使用list(zip(array1,array2))时,我得到了一个不同的结果,我不知道为什么?第一张照片是我期望的,第二张是我得到的

def generate_data(self):

    #For train
    peak_ground_acc_train = np.random.uniform(low=1.0, high=50.0, size=100) #y-axis
    distance_train = np.arange(1, len(peak_ground_acc_train)+1) #x-axis

    magnitude = np.random.uniform(low=3.5, high=9.0, size=100)
    sample_label = magnitude

    #For test
    peak_ground_acc_test = np.random.uniform(low=1.0, high=50.0, size=100) #y-axis
    distance_test = np.arange(0, len(peak_ground_acc_test)) #x-axis

    train_data = list(zip(distance_train, peak_ground_acc_train))
    test_data = list(zip(distance_test, peak_ground_acc_test))

    plt.plot(distance_train, peak_ground_acc_train, 'b')
    plt.plot(train_data, 'r')
    plt.show()

    return train_data, sample_label, test_data

What I expected

What I got


Tags: testdatanptrainrandomuniformziplist
1条回答
网友
1楼 · 发布于 2024-04-29 12:07:03

plot()要求数据参数为标量或一维数组形式的值。请参阅文档中的“参数”: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.plot.html#matplotlib.pyplot.plot

它确实接受二维参数,但将每列视为单独的数据系列。因此,您看到的直线是第1列的绘图,而您看到的其他绘图是第2列的绘图

为什么要将压缩后的数据传递到plot()?为什么不:

plt.plot(distance_train, peak_ground_acc_train, 'r')

相关问题 更多 >