Допустим, у меня есть 2D-массив, такой как:
Z = np.array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 26067, 26067, 26750, 26750, 0, 0, 26673, 26673, 0, 0, 24411, 24411, 0, 0, 45494, 45494, 0],
[ 0, 26067, 26067, 26750, 26750, 0, 0, 26673, 26673, 0, 0, 24411, 24411, 0, 0, 45494, 45494, 0],
[ 0, 26750, 26750, 0, 0, 21237, 21237, 25516, 25516, 25839, 25839, 0, 0, 0, 0, 41704, 41704, 0],
[ 0, 26750, 26750, 0, 0, 21237, 21237, 25516, 25516, 25839, 25839, 0, 0, 0, 0, 41704, 41704, 0],
[ 0, 0, 0, 21236, 21236, 26414, 26414, 0, 0, 22847, 22847, 0, 0, 27051, 27051, 0, 0, 0],
[ 0, 0, 0, 21236, 21236, 26414, 26414, 0, 0, 22847, 22847, 0, 0, 27051, 27051, 0, 0, 0],
[ 0, 26673, 26673, 25516, 25516, 0, 0, 26414, 26414, 0, 0, 0, 0, 45013, 45013, 0, 0, 0],
[ 0, 26673, 26673, 25516, 25516, 0, 0, 26414, 26414, 0, 0, 0, 0, 45013, 45013, 0, 0, 0],
[ 0, 0, 0, 25839, 25839, 22860, 22860, 0, 0, 26213, 26213, 39181, 39181, 0, 0, 0, 0, 0],
[ 0, 0, 0, 25839, 25839, 22860, 22860, 0, 0, 26213, 26213, 39181, 39181, 0, 0, 0, 0, 0],
[ 0, 24411, 24411, 0, 0, 0, 0, 0, 0, 39183, 39183, 0, 0, 0, 0, 0, 0, 0],
[ 0, 24411, 24411, 0, 0, 0, 0, 0, 0, 39183, 39183, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 27052, 27052, 45015, 45015, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 27052, 27052, 45015, 45015, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 45494, 45494, 41434, 41434, 0, 0, 0, 0, 0,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, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5],
[ 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5],
[ 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5],
[ 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5],
[ 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5]])
И я строю поверхность, используя это:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
p = ax.pcolor(X, Y, Z, cmap=cm.plasma) #inferno, plasma, jet, sismic...
fig.colorbar(p)
plt.show()
Я не хочу, чтобы matplotlib строил (или рисовал)большая нижняя восточная область с 0, поэтому вместо цвета, связанного со значением 0, я хочу видеть цвет фона (или прозрачный цвет).
В MATLAB я могу сделать это, назначив NaN
длязначения, которые вы не хотите видеть.Я пытался с math.nan
, но это не работает.Как я могу сделать это в Python 3.6?
Спасибо.