尝试使用 np.where 掩盖某些值
Trying to mask out certain values using np.where
我有一个 3D 数组(可以被认为是时间、y 和 x),我试图屏蔽掉 .4 和 .6 之间的值以显示等值线图,其中所有值都在 .4 到.6 是白色的(或基本上被遮盖了)。
arr_3d = np.random.rand(5,10,20)
plt.pcolormesh(arr_3d[2])
plt.colorbar()
这是我试图掩盖 .4 和 .6 之间的任何值的尝试:
# mask out or turn to zero values that are between .4 and .6
attempt_to_mask = np.where((arr_3d > .4) & (arr_3d < .6),arr_3d == 0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_562130/1333698014.py in <module>
7 # mask out or turn to zero values that are between .4 and .6
8
----> 9 attempt_to_mask = np.where((arr_3d > .4) & (arr_3d < .6),arr_3d == 0)
<__array_function__ internals> in where(*args, **kwargs)
ValueError: either both or neither of x and y should be given
错误是什么意思?我如何为 3d 数组(大小 5,10,20)中的每个时间步屏蔽掉 .4 和 .6 之间的所有值。
你可能想要这样的东西吗?
arr_3d[(arr_3d > .4) & (arr_3d < .6)] = 0
注意:在应用上述代码之前,您可能需要对原始数组进行copy
。
刚刚找到另一种选择(如果你想让它们被屏蔽而不是变成零):
masked = ma.masked_where((arr_3d > .4) & (arr_3d < .6), arr_3d)
上面的答案也很有效:)
我有一个 3D 数组(可以被认为是时间、y 和 x),我试图屏蔽掉 .4 和 .6 之间的值以显示等值线图,其中所有值都在 .4 到.6 是白色的(或基本上被遮盖了)。
arr_3d = np.random.rand(5,10,20)
plt.pcolormesh(arr_3d[2])
plt.colorbar()
这是我试图掩盖 .4 和 .6 之间的任何值的尝试:
# mask out or turn to zero values that are between .4 and .6
attempt_to_mask = np.where((arr_3d > .4) & (arr_3d < .6),arr_3d == 0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/tmp/ipykernel_562130/1333698014.py in <module>
7 # mask out or turn to zero values that are between .4 and .6
8
----> 9 attempt_to_mask = np.where((arr_3d > .4) & (arr_3d < .6),arr_3d == 0)
<__array_function__ internals> in where(*args, **kwargs)
ValueError: either both or neither of x and y should be given
错误是什么意思?我如何为 3d 数组(大小 5,10,20)中的每个时间步屏蔽掉 .4 和 .6 之间的所有值。
你可能想要这样的东西吗?
arr_3d[(arr_3d > .4) & (arr_3d < .6)] = 0
注意:在应用上述代码之前,您可能需要对原始数组进行copy
。
刚刚找到另一种选择(如果你想让它们被屏蔽而不是变成零):
masked = ma.masked_where((arr_3d > .4) & (arr_3d < .6), arr_3d)
上面的答案也很有效:)