numpy min和max函数返回nan

时间:2017-03-30 16:40:40

标签: python matplotlib contour

我有一个二维数组,其中包含浮点数,表示两个其他2D数组之间的差异。我需要创建一个轮廓并对其进行映射,但是当我尝试绘制轮廓时,我会收到一个错误,指出我的轮廓需要至少2个级别。

ValueError:填充轮廓至少需要2个级别。 错误从a = np.min(tsEndAvg)开始,b = np.max(tsEndAvg)

 k = int(raw_input("Enter the iteration amount: "))*12
    x = 0
    # Appending Arrays to cover last 360th slot of lon
    lon2 = np.empty(len(lon)+1)
    for l in range(0, len(lon)):
        lon2[l] = lon[l]
    lon2[len(lon2) - 1] = 360.
    # Creating temp arrays for manipulation later
    tssum = np.empty([len(lat), len(lon)+1])
    tsEndSum = np.empty([len(lat), len(lon) + 1])
    ts2 = np.empty([len(lat), len(lon2)])
    time2 = np.empty(len(time))
    tsEndAvg = np.empty([len(lat), len(lon) + 1])

#START WHILE FOR BEGINNING DATA
    while x < k:
        print str(x)
        # x iterates through a slice of time, k is the iteration amount
        # Attempting to grab global sum of surface temperatures over k iterations
        ts = tempts[x, :, :]
        for i in range(0, len(lat)):
            for j in range(0, len(lon)):
                ts2[i][j] = ts[i][j]
        for i in range(0, 192):
            ts2[i][len(lon2) - 1] = ts[i][0]

        tssum = tssum + ts2# Running sum of the global data
        newDate = beginDate + datetime.timedelta(time[x])
        print newDate
        x = x + 1
    # Dividing each data point in tssum by k for the average amount
#END WHILE
    tssum = tssum / k

#START WHILE FOR END DATA
    x = len(time) -1
    while x > (len(time) - 1) - k:
        # x iterates through a slice of time, k is the iteration amount
        # Attempting to grab global sum of surface temperatures over k iterations
        print x
        ts = tempts[x, :, :]
        for i in range(0, len(lat)):
            for j in range(0, len(lon)):
                ts2[i][j] = ts[i][j]
        for i in range(0, 192):
            ts2[i][len(lon2) - 1] = ts[i][0]
        tsEndSum = tsEndSum + ts2
        x = x - 1
#END WHILE

# AVERAGING ALL GLOBAL DATA
    tsEndSum = tsEndSum / k
    tsEndAvg = tsEndSum - tssum
#END AVG   
    a = np.min(tsEndAvg)
    b = np.max(tsEndAvg)

    plt.figure()
    ax = plt.axes(projection=c.PlateCarree())
    ax.coastlines(resolution='50m', linewidth=.25)
    ax.outline_patch.set_visible(False)
    lon2, lat2 = np.meshgrid(lon2, lat)
    plt.contourf(lon2, lat2, tsEndAvg, levels=np.arange(a,b,1)) #ERROR POINTS TO THIS LINE
    name = "SurfaceTempAnom-"+str(k/12)+".png"
    plt.savefig("/Users/Robert/PycharmProjects/CESM/anoms/"+name, dpi=750, bbox_inches='tight', pad_inches=0)
    print('saved: '+name)

1 个答案:

答案 0 :(得分:0)

好吧,请告诉我们你的输入数据,因为它应该有效。例如,参见最小的例子:

将numpy导入为np A = [[1,2],[3,4] print np.max(a)

它将返回4.最可能的问题是:

  • 输入无法解释为np-array
  • 它不是浮动列表/数组
  • ...