0

我有两个 numpy 数组。一个用于 x 轴条目,另一个用于 y 轴,如下面的代码所示

    plt.figure(figsize=(10, 10))
    plt.plot(range(0,len(TVals_R)),TVals,'bo',markersize=1,label='Dry Run') #I need x and y arrays in different size here
    plt.figure(figsize=(10, 10))
    plt.ylabel('Temperature ($^\circ$C)')
    plt.xlabel('Measurement')
    plt.title("Temperature vs. Measurement")
    plt.legend(loc="upper right")

在 x 轴上,我想使用比 y 数组更大的数字,例如len(TVals_R). 因为我将在图中添加两条具有不同 x 轴范围的线。但它返回错误ValueError: x and y must have same first dimension, but have shapes (920,) and (498,)

有没有办法在 pylot 上使用不同大小的列表?

我还尝试使用不同的轴在图表中添加两条不同大小的线,但由于我有第三条来,我不能使用它。这是我尝试过的

    plt.figure(figsize=(10, 10))
    fig,ax1=plt.subplots()
    ax2=ax1.twiny()
    ax3=ax1.twiny()
    curve1, = ax1.plot(range(0,len(TVals)),TVals,'bo',markersize=1,label='Dry Run')
    curve2, = ax2.plot(range(0,len(TVals_R)),TVals_R,'ro',markersize=1,label='Radiation Run')
    curve3, = ax3.plot(range(0,len(TVals)),TVals_interpolated_R,'go',markersize=1,label='handheld meter and \n linear interpolation')
    curves = [curve1,curve2,curve3]
    ax2.legend(curves, [curve.get_label() for curve in curves]) 
    ax1.set_xlabel('Measurement', color=curve1.get_color()) 
    ax2.set_xlabel('Measurement', color=curve2.get_color())
    ax1.set_ylabel('Temperature ($^\circ$C)')  
    plt.ylabel('Temperature ($^\circ$C)')
    #plt.xlabel('Measurement')
    plt.title("Temperature vs. Measurement")

返回错误ValueError: x and y must have same first dimension, but have shapes (920,) and (498,)

我没有将轴分成 ax1、ax2 等,而是尝试将空元素添加到较小的列表中以匹配最大的列表,但是[]在 numpy 中添加了0 (zero)这在我的数据中会产生误导

我感谢任何一种方法的帮助。

4

1 回答 1

0

看起来这样做的最佳方法是忽略错误,因此可以叠加 2 个具有不同 x 轴范围的图。而且这种解决方案不需要无花果、斧头分离。

    plt.figure(figsize=(10, 10))
    try:
        plt.plot(range(0,len(TVals)),TVals,'o',markersize=1,label='Dry Run')
        plt.plot(range(0,len(TVals_R)),TVals_R,'ro',markersize=1,label='Radiation Run')
        plt.plot(range(0,len(TVals)),TVals_interpolated,'go',markersize=1,label='handheld meter and \n linear interpolation')
    except ValueError:
            pass
    plt.ylabel('Temperature ($^\circ$C)')
    plt.xlabel('Measurement')
    plt.title("Temperature vs. Measurement")
    plt.legend(loc="upper right")
    plt.show()
于 2021-03-15T05:25:25.320 回答