Python 3: поверхность графика без цвета в некоторых точках - PullRequest
0 голосов
/ 11 октября 2018

Допустим, у меня есть 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,     0,     0,     0,     0,     0,     0,     0,     0],
   [    0, 45494, 45494, 41434, 41434,     0,     0,     0,     0,            0,     0,     0,     0,     0,     0,     0,     0,     0],
   [    0,     0,     0,     0,     0,     0,     0,     0,     0,            0,     0,     0,     0,     0,     0,     0,     0,     0]])

X = np.array([[ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5],
   [ 0.5,  0.5,  1.5,  1.5,  2.5,  2.5,  3.5,  3.5,  4.5,  4.5,  5.5,         5.5,  6.5,  6.5,  7.5,  7.5,  8.5,  8.5]])

Y = np.array([[ 0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,         0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5],
   [ 0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,         0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5],
   [ 1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,         1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5],
   [ 1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5,         1.5,  1.5,  1.5,  1.5,  1.5,  1.5,  1.5],
   [ 2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,         2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5],
   [ 2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5,         2.5,  2.5,  2.5,  2.5,  2.5,  2.5,  2.5],
   [ 3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,         3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5],
   [ 3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5,         3.5,  3.5,  3.5,  3.5,  3.5,  3.5,  3.5],
   [ 4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,         4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5],
   [ 4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5,         4.5,  4.5,  4.5,  4.5,  4.5,  4.5,  4.5],
   [ 5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,         5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5],
   [ 5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5,         5.5,  5.5,  5.5,  5.5,  5.5,  5.5,  5.5],
   [ 6.5,  6.5,  6.5,  6.5,  6.5,  6.5,  6.5,  6.5,  6.5,  6.5,  6.5,         6.5,  6.5,  6.5,  6.5,  6.5,  6.5,  6.5],
   [ 6.5,  6.5,  6.5,  6.5,  6.5,  6.5,  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?

Спасибо.

1 Ответ

0 голосов
/ 11 октября 2018

Вместо использования NaN вы можете добиться этого, маскируя ячейки, которые хотите игнорировать.Matplotlib увидит замаскированные значения и просто не отобразит их.

numpy.ma

https://docs.scipy.org/doc/numpy-1.15.1/reference/maskedarray.html

Вы можете маскировать массив, подавая логический массив в виде индексов или маскируя каждую ячейку отдельно;это очень гибкий модуль. Например,

X = np.ma.masked_where(X>0.5, X)

.

РЕДАКТИРОВАТЬ: Я знаю, что это поведение в 2D, хотя я не уверен в 3D.Мне не ясно, к какой проблеме относится ОП.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...