【问题标题】:Array combining in pythonpython中的数组组合
【发布时间】:2020-03-25 05:56:39
【问题描述】:

我尝试将两个单维数组中的值组合起来,以生成类似 [(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

【问题讨论】:

  • 请将您的预期和实际结果作为文本包含在问题中,而不是链接到屏幕截图。
  • list(zip(...)) 做你想做的事,但问题似乎是你的情节是错误的,你只是猜测原因是因为list(zip(...)) 给出了错误的结果。还有什么问题?
  • 这能回答你的问题吗? Converting two lists into a matrix

标签: python


【解决方案1】:

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')

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 2022-11-11
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    相关资源
    最近更新 更多