【发布时间】:2017-08-24 17:22:39
【问题描述】:
我有一个充满浮点数的二维数组,表示其他两个二维数组之间的差异。我需要创建一个等高线并对其进行映射,但是当我尝试绘制等高线时,我收到一条错误消息,指出我的等高线至少需要 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)
【问题讨论】:
-
您的问题标题和问题正文似乎是不同的东西。你是在问为什么你从
np.min或np.max得到nan(在这种情况下,关于轮廓的所有信息似乎都是多余的,但我们需要知道你是如何计算tsEndAvg),或者你对使用np.arange(np.nan, np.nan, 1)你的轮廓水平不起作用?你有什么问题? -
当我试图找到最小值和最大值时,错误是 a 和 b 都是 nan 。奇怪的是,当我一开始在控制台中运行文件时它不起作用,然后当我在控制台中对同一数据集使用 np.min 函数时,它会起作用并返回一个实际值。
-
啊,你使用的是
np.empty。生成的数组的内容本质上是不确定的,可能包含也可能不包含NaNs。您应该仔细检查您是否确实在写入该数组的每个插槽,并考虑改用np.zeros。
标签: python matplotlib contour