user24353751
user24353751

Reputation: 35

3D individual Delaunay cells that change color internally, depending on value of Z (or any other function)

I've got a 3D surface built on Delaunay triangulation. Do you know how to enforce matplotlib to paint each triangle not with a constant color but with a user-defined-function dependent color ? (or at least as a function of z, namely the vertical coord.).

Here is my script, together with my datafile:

# 3D DELAUNAY TRIANGULATION FOR UNSTRUCTURED GRIDS APPLIED TO A DATAFILE

import numpy as np
import matplotlib.tri as mtri
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.collections import LineCollection
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.pyplot as plt
with open('datafile.txt', 'r') as f:
    lines = f.readlines()

    x, y, z = np.genfromtxt("datafile.txt", unpack=True)

ax.scatter(x, y, marker=".", c="#DC143C", edgecolors="black", s=100)
ax.set_xlabel('X')
ax.set_ylabel('Y')

triang = mtri.Triangulation(x, y)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

ax.triplot(triang, c="#D3D3D3", marker='.', markerfacecolor="#DC143C",
    markeredgecolor="black", markersize=5)

ax.set_xlabel('X')
ax.set_ylabel('Y')

isBad = np.where((x<-1) | (x>7) | (y<12) | (y>88), True, False)

mask = np.any(isBad[triang.triangles],axis=1)
triang.set_mask(mask)

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

ax.triplot(triang, c="#D3D3D3", marker='.', markerfacecolor="#DC143C", 
    markeredgecolor="black", markersize=5)

ax.set_xlabel('X')
ax.set_ylabel('Y')

fig, ax = plt.subplots(subplot_kw={'projection':'3d'}, facecolor='lightgray', figsize=(12,8))
fig.subplots_adjust(top=1.7, bottom=-.5, left=-0.05, right=1)
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1, 1, 0.6, 1]))

ax.plot_trisurf(triang, z, cmap='jet', alpha=1) 

ax.set_xlabel('X')
ax.set_xlim(0, 6.7)
ax.set_ylabel('Y')
ax.set_ylim(12, 88)
ax.set_zlabel('Z')
ax.set_zlim(-1.5, 1.8)

plt.show()

My datafile (datafile.txt) is

 0   51  0.2
 0   58  0.3
 0   60  0.3
 0   60.1    0.4
 0   61  0.4
 0   62  0.5
 0   63  0.4
 0   63.1    0.5
 0   64  0.1
 0   64.1    0.3
 0   65  0.2
 0   68  0.5
 0   69  0.5
 0   70  0.5
 0   72  0.2
 0   72.1    0.3
 0   73  0.3
 0   73.1    0.1
 0   73.2    0.3
 0   74  0.3
 0   74.1    0.3
 0   75  0.1
 0   75.1    0.9
 0   76  0.2
 0   76.1    0.7
 0   76.2    0.9
 0   77  0.4
 0   79  0.2
 0   81  0.1
 0   81.1    0.2
 0   81.2    0.2
 0   81.3    0.2
 0   81.4    0.1
 0   83  0.1
 0   83.1    0.3
 0   84  0.4
 0.1     41  0.3
 0.1     46  0.5
 0.1     47  0.1
 0.1     53  0.3
 0.1     53.1    0.5
 0.1     57  0.5
 0.1     59  0.3
 0.1     61  0.3
 0.1     61.1    0.1
 0.1     61.2    -0.1
 0.1     62  0.3
 0.1     62.1    0.1
 0.1     62.2    0.1
 0.1     63  -0.3
 0.1     64  0.2
 0.1     64.1    0.6
 0.1     64.2    -0.1
 0.1     65  0.2
 0.1     65.1    0.1
 0.1     65.2    0.8
 0.1     65.3    0.9
 0.1     67  0.4
 0.1     68  0.3
 0.1     69  0.2
 0.1     70  0.3
 0.1     70.1    0.1
 0.1     71  0.1
 0.1     71.1    0.1
 0.1     72  0.2
 0.1     73  0.2
 0.1     73.1    0
 0.1     73.2    0.6
 0.1     73.3    0.3
 0.1     73.4    0.2
 0.1     75  0.4
 0.1     78  0.8
 0.1     78.1    0.2
 0.1     78.2    0.8
 0.1     79  0.5
 0.1     79.1    0.4
 0.1     82  0
 0.1     83  0.5
 0.2     34  0.2
 0.2     39  0.6
 0.2     42  0.5
 0.2     43  0.6
 0.2     49  0.4
 0.2     49.1    0.4
 0.2     50  0.7
 0.2     54  0.4
 0.2     54.1    0.6
 0.2     58  0.4
 0.2     58.1    0.1
 0.2     59  0
 0.2     60  0.6
 0.2     63  0.2
 0.2     63.1    0.5
 0.2     63.2    0.3
 0.2     64  -0.2
 0.2     64.1    0
 0.2     67  0.5
 0.2     69  0.5
 0.2     69.1    0.3
 0.2     71  0.2
 0.2     71.1    0
 0.2     71.2    0.1
 0.2     72  -0.1
 0.2     72.1    0.2
 0.2     74  -0.1
 0.2     75  0.2
 0.2     76  0.2
 0.2     77  0.4
 0.2     79  0.2
 0.2     79.1    0.2
 0.2     81  0.2
 0.2     81.1    0.3
 0.3     35  0.1
 0.3     37  0.2
 0.3     41  0.8
 0.3     46  0.4
 0.3     51  0.7
 0.3     53  0.7
 0.3     54  0.2
 0.3     57  0.1
 0.3     58  0.6
 0.3     59  0.5
 0.3     61  0.3
 0.3     61.1    0.1
 0.3     62  0.5
 0.3     62.1    0.8
 0.3     62.2    0.3
 0.3     63  0.3
 0.3     63.1    0.3
 0.3     64  0.8
 0.3     64.1    0.3
 0.3     64.2    0.4
 0.3     69  0.6
 0.3     70  0.3
 0.3     72  0.1
 0.3     74  0
 0.3     76  0.3
 0.3     76.1    0.1
 0.3     79  0.3
 0.3     80  0.1
 0.3     80.1    0.1
 0.3     81  0.3
 0.3     81.1    0.2
 0.3     83  0
 0.4     34  0.6
 0.4     34.1    0.5
 0.4     36  0.2
 0.4     39  0.6
 0.4     41  -0.1
 0.4     42  0.3
 0.4     43  0.5
 0.4     48  0.7
 0.4     49  0.6
 0.4     52  0.8
 0.4     52.1    0.1
 0.4     54  0.7
 0.4     58  0.8
 0.4     60  0
 0.4     61  0.1
 0.4     63  -0.1
 0.4     63.1    0
 0.4     66  0.7
 0.4     67  0.2
 0.4     67.1    0.3
 0.4     70  0.4
 0.4     70.1    0.3
 0.4     71  0.1
 0.4     74  0.2
 0.5     33  0.6
 0.5     34  0.1
 0.5     35  0.3
 0.5     46  0.1
 0.5     47  0.4
 0.5     53  0.8
 0.5     55  0.3
 0.5     58  0.1
 0.5     59  0.2
 0.5     59.1    0.1
 0.5     60  0.8
 0.5     60.1    -0.1
 0.5     61  0.4
 0.5     61.1    0.3
 0.5     62  0.2
 0.5     63  0
 0.5     64  0.2
 0.5     67  0.2
 0.5     68  0.2
 0.5     69  0.2
 0.5     70  0.1
 0.5     72  0.1
 0.5     73  0.1
 0.5     76  0.3
 0.5     76.1    0.2
 0.5     79  0.2
 0.5     79.1    0.2
 0.5     80  0.8
 0.5     84  0.1
 0.5     84.1    0.4
 0.6     20  0.7
 0.6     22  0.8
 0.6     30  0.4
 0.6     30.1    0.7
 0.6     31  0.6
 0.6     35  0.9
 0.6     35.1    0.9
 0.6     37  0.5
 0.6     38  0.2
 0.6     39  0.6
 0.6     42  0.3
 0.6     43  0.3
 0.6     46  1
 0.6     47  0.6
 0.6     49  0.2
 0.6     49.1    0.1
 0.6     51  0.9
 0.6     51.1    0.2
 0.6     54  0.9
 0.6     55  1.1
 0.6     59  0.6
 0.6     61  0.6
 0.6     62  0.6
 0.6     62.1    0.6
 0.6     62.2    0.1
 0.6     64  0.9
 0.6     65  0.2
 0.6     65.1    0.5
 0.6     66  0.2
 0.6     75  0.4
 0.6     81  0.1
 0.6     81.1    0.1
 0.6     81.2    0.7
 0.6     83  0.5
 0.7     23  0.4
 0.7     24  0.9
 0.7     26  1.1
 0.7     27  0.7
 0.7     33  0.7
 0.7     33.1    0
 0.7     34  0.3
 0.7     36  0.1
 0.7     37  0.9
 0.7     37.1    0.6
 0.7     40  0.5
 0.7     46  0.1
 0.7     46.1    0.8
 0.7     46.2    0.5
 0.7     48  0.7
 0.7     49  0.5
 0.7     50  0.9
 0.7     51  0.8
 0.7     52  0.8
 0.7     52.1    0.5
 0.7     54  0.2
 0.7     54.1    0
 0.7     55  0.9
 0.7     56  0.6
 0.7     56.1    0.6
 0.7     56.2    0.8
 0.7     56.3    0.1
 0.7     57  0.4
 0.7     58  0
 0.7     59  1
 0.7     59.1    0
 0.7     60  0.1
 0.7     62  0.2
 0.7     68  0.1
 0.7     68.1    0.2
 0.7     71  0.4
 0.7     71.1    0.5
 0.7     72  0.2
 0.7     77  0.2
 0.7     81  0.2
 0.7     83  0.1
 0.8     21  1
 0.8     28  0.9
 0.8     28.1    0.1
 0.8     29  0.8
 0.8     32  0.1
 0.8     34  1.1
 0.8     37  0.1
 0.8     38  0
 0.8     39  0.7
 0.8     45  0.2
 0.8     47  0.1
 0.8     47.1    0.3
 0.8     47.2    0.7
 0.8     48  0.2
 0.8     50  0.2
 0.8     50.1    0.8
 0.8     52  0.2
 0.8     52.1    0.2
 0.8     54  0.2
 0.8     58  1
 0.8     60  0.2
 0.8     62  0.5
 0.8     64  0.2
 0.8     64.1    0.2
 0.8     65  0.6
 0.8     65.1    0.2
 0.8     65.2    0.2
 0.8     65.3    0.3
 0.8     66  0
 0.8     68  0.4
 0.8     71  0
 0.8     71.1    0.4
 0.8     73  0.3
 0.8     76  0.3
 0.8     77  0.2
 0.8     77.1    0.1
 0.8     77.2    0.1
 0.8     78  0.3
 0.8     81  0.2
 0.8     81.1    0.3
 0.8     84  0.2
 0.9     21  0.9
 0.9     21.1    1.1
 0.9     22  0.8
 0.9     24  0.4
 0.9     26  0.9
 0.9     29  0.7
 0.9     32  0.9
 0.9     32.1    0.7
 0.9     35  0.7
 0.9     36  1.4
 0.9     38  0
 0.9     41  0
 0.9     43  1
 0.9     44  0.7
 0.9     54  0.2
 0.9     56  0.7
 0.9     57  1.2
 0.9     60  0.3
 0.9     60.1    1
 0.9     61  0.2
 0.9     62  1.1
 0.9     62.1    0.3
 0.9     62.2    0.4
 0.9     64  0.3
 0.9     64.1    0.2
 0.9     67  0.2
 0.9     70  0.2
 0.9     70.1    0.5
 0.9     72  0.3
 0.9     76  0.2
 0.9     77  0.1
 0.9     78  0.1
 0.9     78.1    0.3
 0.9     80  0
 0.9     80.1    0.3
 1   24  0.3
 1   25  0.7
 1   30  0.6
 1   30.1    0.9
 1   34  0.9
 1   34.1    0.7
 1   35  0.9
 1   36  0.6
 1   37  0.2
 1   38  0.6
 1   39  1
 1   40  0
 1   40.1    1
 1   43  0.6
 1   48  0.5
 1   48.1    1.1
 1   50  0.1
 1   51  0.8
 1   52  1.1
 1   53  0.1
 1   54  0.4
 1   55  0.1
 1   58  0.2
 1   60  0.2
 1   60.1    0.3
 1   61  0
 1   61.1    0.9
 1   62  0.1
 1   64  0.4
 1   65  0.2
 1   66  0.3
 1   68  0.5
 1   69  0.9
 1   70  1.5
 1   71  0
 1   72  0.1
 1   72.1    0.2
 1   73  0.2
 1   73.1    1.1
 1   73.2    0.2
 1   74  0.2
 1   74.1    0.3
 1   76  0.2
 1   77  0.4
 1   77.1    0.2
 1   77.2    0.2
 1   81  0.5
 1   85  0.4
 1.1     22  1.2
 1.1     23  0.7
 1.1     23.1    0.5
 1.1     24  0.7
 1.1     24.1    0.8
 1.1     26  0.9
 1.1     26.1    1.2
 1.1     33  1.3
 1.1     34  1.2
 1.1     36  0.7
 1.1     40  0.6
 1.1     42  0
 1.1     46  0.4
 1.1     48  0.2
 1.1     49  0.1
 1.1     52  0.1
 1.1     55  0.2
 1.1     57  1.2
 1.1     59  1.4
 1.1     60  1.5
 1.1     60.1    0
 1.1     61  0.1
 1.1     61.1    -0.1
 1.1     61.2    1
 1.1     62  0.2
 1.1     64  0.1
 1.1     64.1    0.2
 1.1     64.2    0.2
 1.1     64.3    0.1
 1.1     65  0.7
 1.1     65.1    0.4
 1.1     66  0.9
 1.1     66.1    0.3
 1.1     66.2    0.2
 1.1     67  0.2
 1.1     67.1    0.1
 1.1     68  0.4
 1.1     71  0.5
 1.1     71.1    0.1
 1.1     71.2    0.2
 1.1     75  0.2
 1.1     76  0.3
 1.1     76.1    0.4
 1.1     77  0.3
 1.1     77.1    0.4
 1.1     79  0.2
 1.2     22  1.2
 1.2     22.1    0.9
 1.2     23  1.3
 1.2     28  0
 1.2     30  0.8
 1.2     32  0
 1.2     33  0.8
 1.2     33.1    0.5
 1.2     35  0.8
 1.2     35.1    1.1
 1.2     38  0.1
 1.2     38.1    0.1
 1.2     39  1
 1.2     39.1    0.7
 1.2     39.2    0.8
 1.2     40  0.3
 1.2     42  1
 1.2     43  1.5
 1.2     44  0
 1.2     47  0.5
 1.2     47.1    0.6
 1.2     47.2    0.4
 1.2     47.3    0.2
 1.2     49  0
 1.2     49.1    0.2
 1.2     50  0
 1.2     50.1    0
 1.2     53  0.1
 1.2     53.1    1.2
 1.2     58  0.3
 1.2     58.1    0
 1.2     60  0.7
 1.2     63  0.1
 1.2     63.1    0.3
 1.2     65  0.2
 1.2     65.1    -0.1
 1.2     66  0.4
 1.2     66.1    0.1
 1.2     66.2    0.2
 1.2     67  0.2
 1.2     67.1    0.3
 1.2     69  0.1
 1.2     70  0.2
 1.2     70.1    0
 1.2     71  0.2
 1.2     75  0.1
 1.2     78  0.1
 1.2     80  0.3
 1.2     81  0
 1.2     81.1    0.2
 1.2     81.2    0.4
 1.2     82  0.3
 1.2     82.1    0.1
 1.2     83  0.1
 1.3     18  0.6
 1.3     22  0.6
 1.3     22.1    0.8
 1.3     22.2    0.6
 1.3     24  0.3
 1.3     24.1    1
 1.3     29  0.6
 1.3     30  1.2
 1.3     32  1.1
 1.3     34  0.9
 1.3     37  0.3
 1.3     37.1    0.9
 1.3     38  0.2
 1.3     41  1.4
 1.3     45  -0.1
 1.3     45.1    1.1
 1.3     45.2    1.1
 1.3     47  0.1
 1.3     52  0.5
 1.3     59  0.1
 1.3     59.1    0.2
 1.3     60  0.2
 1.3     61  0.9
 1.3     62  0.2
 1.3     63  0.1
 1.3     64  0
 1.3     65  1.1
 1.3     67  0.1
 1.3     67.1    0.2
 1.3     68  0.3
 1.3     68.1    0.1
 1.3     69  1.2
 1.3     71  0.6
 1.3     72  0.2
 1.3     73  0.2
 1.3     73.1    0.3
 1.3     74  0.1
 1.3     75  1.2
 1.3     77  0.3
 1.3     78  0.2
 1.3     79  0.2
 1.3     84  0.2
 1.4     21  1.3
 1.4     21.1    0.8
 1.4     22  0.7
 1.4     23  1.3
 1.4     28  0.5
 1.4     29  0.9
 1.4     31  1.3
 1.4     33  0.6
 1.4     35  0.1
 1.4     38  0.2
 1.4     41  0.1
 1.4     44  0.5
 1.4     46  0.2
 1.4     47  0.1
 1.4     50  0.3
 1.4     51  0
 1.4     53  0.2
 1.4     53.1    0.2
 1.4     54  0.1
 1.4     55  0.8
 1.4     57  0.2
 1.4     59  0.3
 1.4     59.1    0.2
 1.4     62  0.6
 1.4     63  1.2
 1.4     64  0.5
 1.4     65  0
 1.4     66  0.2
 1.4     67  0.5
 1.4     67.1    1.2
 1.4     69  0.6
 1.4     69.1    0.1
 1.4     71  0.3
 1.4     71.1    0.1
 1.4     73  0.1
 1.4     74  0.3
 1.4     74.1    0.4
 1.4     75  0.2
 1.4     75.1    0.3
 1.4     75.2    0.2
 1.4     75.3    0.2
 1.4     77  0.2
 1.4     77.1    0.3
 1.4     78  0.2
 1.4     78.1    0.3
 1.4     81  0.2
 1.4     83  0
 1.4     85  0.3
 1.5     20  0.6
 1.5     21  0.7
 1.5     21.1    1.1
 1.5     23  1.3
 1.5     26  0.9
 1.5     28  0.9
 1.5     32  1.2
 1.5     32.1    1.8
 1.5     33  0.3
 1.5     36  0.1
 1.5     38  0.1
 1.5     38.1    0.1
 1.5     43  0.2
 1.5     43.1    0
 1.5     44  0.5
 1.5     44.1    0.2
 1.5     44.2    0.3
 1.5     44.3    0.1
 1.5     47  0.2
 1.5     49  0.1
 1.5     52  0.1
 1.5     53  0.1
 1.5     54  0.1
 1.5     55  0.2
 1.5     56  0.3
 1.5     57  0.1
 1.5     58  0.2
 1.5     59  1.2
 1.5     61  0.3
 1.5     62  0.2
 1.5     62.1    0.3
 1.5     64  0.5
 1.5     65  0.1
 1.5     65.1    0.2
 1.5     65.2    0.4
 1.5     65.3    0.3
 1.5     66  1
 1.5     67  0.3
 1.5     69  0.3
 1.5     69.1    -0.1
 1.5     70  0.3
 1.5     74  0.3
 1.5     74.1    0.4
 1.5     74.2    0.2
 1.5     74.3    0.3
 1.5     76  0.4
 1.5     77  0.1
 1.5     79  0.2
 1.5     80  0.4
 1.5     83  0.1
 1.6     19  1
 1.6     22  0.9
 1.6     23  1
 1.6     24  1.2
 1.6     26  0.7
 1.6     29  0.9
 1.6     29.1    0.9
 1.6     30  0.1
 1.6     30.1    1.5
 1.6     32  1
 1.6     33  1
 1.6     42  0.8
 1.6     43  0.2
 1.6     43.1    1.1
 1.6     44  0.1
 1.6     48  1.2
 1.6     49  1.1
 1.6     50  -0.1
 1.6     54  0.2
 1.6     54.1    -0.1
 1.6     56  1.3
 1.6     57  0
 1.6     58  0.3
 1.6     59  0.2
 1.6     59.1    0.3
 1.6     61  0.2
 1.6     62  0
 1.6     62.1    1.2
 1.6     65  0.1
 1.6     65.1    0.8
 1.6     65.2    0.1
 1.6     66  0.2
 1.6     67  0.1
 1.6     68  0.2
 1.6     69  0.1
 1.6     70  0.1
 1.6     71  0.4
 1.6     71.1    0
 1.6     71.2    0.2
 1.6     76  1.1
 1.6     83  0.2
 1.6     84  0.4
 1.7     19  1
 1.7     24  0.9
 1.7     24.1    1.2
 1.7     25  0.5
 1.7     26  0.2
 1.7     26.1    0.9
 1.7     28  0.8
 1.7     32  0.7
 1.7     33  1.1
 1.7     38  0.7
 1.7     40  0.1
 1.7     41  0.2
 1.7     41.1    1.2
 1.7     42  0.8
 1.7     45  0.4
 1.7     46  0.1
 1.7     47  0.6
 1.7     53  0.1
 1.7     54  0.2
 1.7     54.1    1.3
 1.7     56  0.1
 1.7     61  0.1
 1.7     62  0.3
 1.7     62.1    1.2
 1.7     62.2    0.6
 1.7     63  1.3
 1.7     63.1    1.3
 1.7     65  -0.5
 1.7     65.1    0.1
 1.7     65.2    0.1
 1.7     66  0.2
 1.7     66.1    0.2
 1.7     67  0.2
 1.7     68  0.1
 1.7     69  0.6
 1.7     71  0.2
 1.7     72  0.9
 1.7     74  0.5
 1.7     76  0.5
 1.7     76.1    0.1
 1.7     78  0.8
 1.7     80  0.3
 1.7     82  0.1
 1.8     21  0.7
 1.8     22  1.2
 1.8     22.1    0.8
 1.8     23  1.4
 1.8     23.1    0.6
 1.8     26  0.9
 1.8     30  0.1
 1.8     31  0.1
 1.8     32  0.9
 1.8     35  0.3
 1.8     39  1.2
 1.8     41  0.2
 1.8     42  0.2
 1.8     42.1    0.2
 1.8     45  0
 1.8     45.1    0.1
 1.8     45.2    -0.1
 1.8     48  0.7
 1.8     56  0.6
 1.8     56.1    0.5
 1.8     57  0.1
 1.8     57.1    0.2
 1.8     58  0
 1.8     59  0.2
 1.8     59.1    0.1
 1.8     59.2    0.9
 1.8     60  0.3
 1.8     60.1    0.1
 1.8     60.2    0.2
 1.8     61  -0.1
 1.8     63  0.2
 1.8     64  0.1
 1.8     65  0.2
 1.8     65.1    0.2
 1.8     65.2    0.2
 1.8     66  0.2
 1.8     66.1    0.6
 1.8     66.2    0.2
 1.8     66.3    0.1
 1.8     66.4    0.2
 1.8     67  0.2
 1.8     68  0.1
 1.8     68.1    0.1
 1.8     70  0.2
 1.8     70.1    0
 1.8     71  0.1
 1.8     71.1    0.1
 1.8     72  0.1
 1.8     72.1    0.3
 1.8     75  0.2
 1.8     75.1    0.3
 1.8     77  0.4
 1.8     79  0.4
 1.8     83  0.1
 1.9     20  0.7
 1.9     23  1.5
 1.9     30  0.8
 1.9     31  0.9
 1.9     34  0.2
 1.9     36  0.1
 1.9     38  0.5
 1.9     38.1    1.1
 1.9     39  0.3
 1.9     40  0
 1.9     41  0.6
 1.9     44  1.3
 1.9     45  0.1
 1.9     46  0.3
 1.9     47  0.1
 1.9     50  0.2
 1.9     50.1    0.5
 1.9     53  0.4
 1.9     53.1    1.1
 1.9     54  0.2
 1.9     58  0.9
 1.9     58.1    0.2
 1.9     58.2    0.2
 1.9     59  0.3
 1.9     59.1    0.1
 1.9     61  0.1
 1.9     62  0.3
 1.9     62.1    0.3
 1.9     63  0.1
 1.9     66  0.2
 1.9     67  0.4
 1.9     68  0.1
 1.9     70  0.2
 1.9     70.1    0.3
 1.9     70.2    0.2
 1.9     70.3    0.1
 1.9     72  0.2
 1.9     72.1    0.2
 1.9     73  0.1
 1.9     74  0.3
 1.9     74.1    0.3
 1.9     75  0.8
 1.9     83  0
 1.9     84  0.4
 1.9     85  0.3
 2   20  1
 2   27  0.4
 2   29  1.4
 2   31  0.8
 2   32  1
 2   32.1    0.3
 2   33  0.3
 2   36  1.2
 2   36.1    0.1
 2   37  0.1
 2   38  0.2
 2   39  0.5
 2   40  0.6
 2   40.1    1.5
 2   43  -0.1
 2   44  0.2
 2   45  0.2
 2   46  -0.1
 2   52  0.1
 2   55  0.5
 2   56  0.5
 2   57  0.2
 2   58  1.1
 2   58.1    0.3
 2   59  0.4
 2   62  0.4
 2   62.1    0
 2   63  0.2
 2   63.1    0.7
 2   64  0.3
 2   65  0.1
 2   65.1    0.1
 2   66  0.2
 2   68  0.2
 2   69  1.2
 2   69.1    0.1
 2   71  0.1
 2   71.1    0.3
 2   71.2    0.1
 2   72  0.2
 2   74  0.2
 2   74.1    1.2
 2   74.2    0.4
 2   74.3    0.2
 2   77  0.1
 2   80  0.2
 2   80.1    0.1
 2   81  0.1
 2.1     21  0.8
 2.1     21.1    0.9
 2.1     25  0.9
 2.1     27  0.9
 2.1     28  0.9
 2.1     29  1.2
 2.1     30  0.9
 2.1     30.1    0.4
 2.1     32  0.8
 2.1     33  0.8
 2.1     34  0.2
 2.1     34.1    0.1
 2.1     40  0.6
 2.1     40.1    0.3
 2.1     42  1
 2.1     43  1.2
 2.1     43.1    1.2
 2.1     44  0.2
 2.1     47  0.2
 2.1     49  0.2
 2.1     52  0.4
 2.1     53  1.3
 2.1     53.1    0.4
 2.1     53.2    0.3
 2.1     55  0.1
 2.1     56  0.1
 2.1     58  0.4
 2.1     59  0.2
 2.1     60  0.1
 2.1     65  0.2
 2.1     66  0.3
 2.1     66.1    0.2
 2.1     66.2    0.3
 2.1     67  0.3
 2.1     68  0.1
 2.1     68.1    0.4
 2.1     69  0.4
 2.1     70  0.9
 2.1     70.1    0.3
 2.1     70.2    -0.1
 2.1     73  0
 2.1     73.1    0.3
 2.1     73.2    0.3
 2.1     73.3    0.1
 2.1     74  0.1
 2.1     75  0.2
 2.1     76  0.5
 2.1     76.1    0.2
 2.1     78  0.4
 2.1     79  0.4
 2.1     79.1    0.4
 2.1     80  0.2
 2.2     18  0.7
 2.2     21  0.9
 2.2     29  0.7
 2.2     29.1    1.1
 2.2     31  0.1
 2.2     32  0.1
 2.2     33  0.3
 2.2     36  0
 2.2     36.1    1.1
 2.2     37  0.9
 2.2     41  0.8
 2.2     41.1    1.5
 2.2     42  0.1
 2.2     44  0.1
 2.2     45  1
 2.2     48  0.3
 2.2     54  0.2
 2.2     54.1    1
 2.2     55  0.2
 2.2     56  0.1
 2.2     56.1    0.1
 2.2     61  0
 2.2     64  0.2
 2.2     64.1    0.2
 2.2     65  0.1
 2.2     66  0.2
 2.2     66.1    0.2
 2.2     67  0.2
 2.2     68  0.1
 2.2     68.1    0.1
 2.2     69  0.1
 2.2     69.1    0.3
 2.2     71  0.2
 2.2     72  0.2
 2.2     73  0.5
 2.2     73.1    0.2
 2.2     74  0.3
 2.2     75  0.9
 2.3     19  0.7
 2.3     37  0.9
 2.3     37.1    0.1
 2.3     37.2    0.2
 2.3     40  0.2
 2.3     41  0.1
 2.3     42  1.2
 2.3     44  0.2
 2.3     45  0.6
 2.3     46  0.2
 2.3     46.1    0.2
 2.3     47  0.5
 2.3     49  0.4
 2.3     50  0.1
 2.3     50.1    0.3
 2.3     54  0.1
 2.3     54.1    0.1
 2.3     55  -0.1
 2.3     56  0.2
 2.3     56.1    0.9
 2.3     56.2    0.2
 2.3     57  0.7
 2.3     57.1    1.4
 2.3     60  0.3
 2.3     61  0.3
 2.3     62  0.2
 2.3     64  0.1
 2.3     64.1    0.3
 2.3     65  0.2
 2.3     66  0.2
 2.3     67  0.3
 2.3     67.1    0.2
 2.3     68  0.5
 2.3     71  0.3
 2.3     71.1    0.3
 2.3     73  0.2
 2.3     74  0.2
 2.3     74.1    0.2
 2.4     22  1.6
 2.4     27  0.7
 2.4     27.1    1
 2.4     27.2    0.1
 2.4     30  0.1
 2.4     37  0.1
 2.4     38  0.7
 2.4     40  0.2
 2.4     41  0.2
 2.4     42  0.2
 2.4     47  1.7
 2.4     51  -0.2
 2.4     52  0.1
 2.4     54  0.2
 2.4     54.1    0.1
 2.4     54.2    0
 2.4     54.3    0.2
 2.4     55  0.5
 2.4     55.1    0.1
 2.4     57  0.2
 2.4     58  0.2
 2.4     59  0.2
 2.4     63  0.1
 2.4     64  0.2
 2.4     65  0.2
 2.4     66  0.2
 2.4     66.1    0.2
 2.4     66.2    0.5
 2.4     67  0.1
 2.4     67.1    0.2
 2.4     68  0.3
 2.4     70  0.3
 2.4     71  0.1
 2.4     75  0.4
 2.4     75.1    0
 2.5     23  0.7
 2.5     26  0.1
 2.5     27  1
 2.5     29  0.8
 2.5     30  0.4
 2.5     31  0.5
 2.5     32  0.9
 2.5     33  1.1
 2.5     34  1.1
 2.5     41  1
 2.5     42  0.1
 2.5     45  0.1
 2.5     45.1    1.3
 2.5     45.2    0.7
 2.5     46  0.1
 2.5     48  1.3
 2.5     48.1    0.5
 2.5     48.2    0.5
 2.5     49  1
 2.5     58  0.2
 2.5     58.1    0.1
 2.5     61  0.2
 2.5     61.1    0.1
 2.5     63  0.2
 2.5     63.1    0.2
 2.5     64  0.2
 2.5     65  0.4
 2.5     66  0.2
 2.5     66.1    0.2
 2.5     67  0.2
 2.5     70  0.3
 2.5     70.1    0.4
 2.5     70.2    0.2
 2.5     70.3    0.2
 2.5     70.4    0.3
 2.5     70.5    0.6
 2.5     73  0.2
 2.5     73.1    0.1
 2.5     73.2    0.3
 2.5     75  0.2
 2.6     22  1
 2.6     24  0.8
 2.6     29  0.5
 2.6     31  0.3
 2.6     31.1    0.5
 2.6     38  0.7
 2.6     38.1    0.1
 2.6     42  0.2
 2.6     43  0.2
 2.6     45  0.1
 2.6     54  0.4
 2.6     54.1    0.2
 2.6     60  0
 2.6     60.1    0.4
 2.6     61  0.2
 2.6     67  0.1
 2.6     68  0.4
 2.6     70  1.2
 2.6     70.1    0.1
 2.6     72  0.1
 2.6     75  0.2
 2.6     79  0.1
 2.7     27  0.7
 2.7     27.1    0.8
 2.7     30  0.3
 2.7     33  1
 2.7     33.1    -0.2
 2.7     38  0.9
 2.7     42  0.7
 2.7     42.1    0.7
 2.7     48  1
 2.7     48.1    0
 2.7     49  0.9
 2.7     53  0.1
 2.7     55  0.4
 2.7     55.1    0.2
 2.7     56  0.1
 2.7     58  0.3
 2.7     60  0.2
 2.7     61  0.5
 2.7     62  0.2
 2.7     64  0.8
 2.7     66  0.1
 2.7     68  0.2
 2.7     69  0.2
 2.7     69.1    0.3
 2.7     72  0.3
 2.7     72.1    0.3
 2.7     73  0.3
 2.7     74  0.2
 2.7     75  0.1
 2.8     17  0.6
 2.8     23  0.4
 2.8     24  0.6
 2.8     27  1.6
 2.8     28  0.6
 2.8     29  0.6
 2.8     31  0.9
 2.8     32  0.9
 2.8     36  0.5
 2.8     38  0.1
 2.8     40  0.6
 2.8     40.1    0.5
 2.8     53  0.4
 2.8     54  0.1
 2.8     54.1    0.2
 2.8     56  0.2
 2.8     56.1    0.2
 2.8     58  0.1
 2.8     58.1    0.7
 2.8     61  1.1
 2.8     64  0
 2.8     65  0.1
 2.8     65.1    0.1
 2.8     68  0.5
 2.8     74  0.8
 2.8     77  0.3
 2.9     19  0.3
 2.9     32  0.1
 2.9     32.1    0.7
 2.9     34  0
 2.9     36  0.1
 2.9     37  0.8
 2.9     38  1
 2.9     38.1    0.8
 2.9     40  1
 2.9     45  1
 2.9     45.1    0.2
 2.9     46  0.3
 2.9     47  0.7
 2.9     53  0.4
 2.9     53.1    0
 2.9     54  0.4
 2.9     55  0.3
 2.9     56  0.1
 2.9     56.1    0.2
 2.9     60  -0.2
 2.9     61  0
 2.9     62  0.2
 2.9     62.1    0.1
 2.9     63  0.2
 2.9     67  0
 2.9     68  0.3
 2.9     73  0.3
 2.9     73.1    0.2
 3   22  1
 3   24  0.1
 3   24.1    0.4
 3   29  0.6
 3   34  0.2
 3   39  0.2
 3   41  0.4
 3   41.1    0.7
 3   42  1
 3   42.1    0.8
 3   56  0.2
 3   65  0.2
 3   69  0.3
 3   71  0.5
 3   78  0.2
 3   79  0.1
 3.1     17  0.8
 3.1     18  0.5
 3.1     22  1
 3.1     32  0.2
 3.1     32.1    0.9
 3.1     38  0.3
 3.1     44  0.5
 3.1     46  0.2
 3.1     53  0.1
 3.1     56  0.1
 3.1     56.1    0.2
 3.1     64  0
 3.1     70  0.4
 3.1     72  0.6
 3.1     76  0.2
 3.2     18  0.7
 3.2     21  0.3
 3.2     22  1.1
 3.2     36  1.2
 3.2     38  0.6
 3.2     38.1    0.8
 3.2     42  0
 3.2     46  -0.1
 3.2     46.1    0.1
 3.2     47  0.3
 3.2     47.1    0.5
 3.2     47.2    0.5
 3.2     50  0.5
 3.2     58  0.1
 3.2     60  0.5
 3.2     65  0.8
 3.2     67  0
 3.2     69  0.9
 3.2     73  1
 3.2     74  0
 3.2     77  0.2
 3.3     17  0.6
 3.3     19  1.5
 3.3     23  1
 3.3     25  0.3
 3.3     28  0.7
 3.3     35  0.5
 3.3     35.1    0.6
 3.3     52  0.2
 3.3     62  0.1
 3.3     63  0.4
 3.3     64  0
 3.3     64.1    0.3
 3.3     64.2    0.1
 3.3     73  0.2
 3.3     74  0.3
 3.3     79  0.2
 3.4     18  0.6
 3.4     20  0.1
 3.4     34  0.6
 3.4     38  0.2
 3.4     38.1    0.6
 3.4     40  0.2
 3.4     41  0.8
 3.4     56  0.7
 3.5     22  1
 3.5     30  0.4
 3.5     30.1    0.4
 3.5     32  0.3
 3.5     34  0.4
 3.5     38  0.8
 3.5     38.1    0.2
 3.5     41  0.5
 3.5     49  0.3
 3.5     54  0.1
 3.5     69  0.2
 3.5     71  0.3
 3.5     74  0.4
 3.6     24  1.1
 3.6     25  0.7
 3.6     32  0.4
 3.6     42  0
 3.6     46  0.2
 3.6     57  0.2
 3.7     22  1.1
 3.7     22.1    0.8
 3.7     23  0.1
 3.7     23.1    1.1
 3.7     27  0.7
 3.7     30  0.6
 3.7     34  0.1
 3.7     35  0.1
 3.7     40  0.9
 3.7     44  0.2
 3.7     45  0.4
 3.7     46  0.5
 3.7     49  0.4
 3.7     50  0.1
 3.7     51  0.8
 3.7     66  0.4
 3.8     18  0.3
 3.8     22  1.4
 3.8     23  0.5
 3.8     24  0.7
 3.8     24.1    0.5
 3.8     28  0.3
 3.8     28.1    0.6
 3.8     37  1.1
 3.8     38  0.6
 3.8     39  0.6
 3.8     44  0.8
 3.8     46  0
 3.8     65  0.1
 3.9     17  1.4
 3.9     27  0.6
 3.9     31  1
 3.9     32  0.5
 3.9     34  0.3
 3.9     54  0.1
 3.9     61  0.2
 3.9     64  0.3
 3.9     66  0.2
 3.9     66.1    0.2
 3.9     67  0
 4   22  1
 4   28  0.6
 4   29  0.4
 4   30  0.2
 4   31  0.6
 4   32  0.1
 4   45  0.5
 4   56  0.2
 4   65  0.1
 4   73  0.3
 4.1     16  1.3
 4.1     23  1.1
 4.1     25  0.6
 4.1     26  0.6
 4.1     28  0.7
 4.1     32  0.4
 4.1     32.1    0.6
 4.1     33  0.5
 4.1     41  0.2
 4.1     42  0.7
 4.1     43  0.2
 4.1     45  0.3
 4.1     64  0.2
 4.1     65  0
 4.2     26  0.5
 4.2     27  0.4
 4.2     27.1    0.7
 4.2     27.2    0.3
 4.2     31  0.5
 4.2     33  0.3
 4.2     33.1    0.5
 4.2     36  0.3
 4.2     38  0.1
 4.2     51  0.3
 4.2     62  0.9
 4.2     70  0.5
 4.3     17  1.2
 4.3     27  0.6
 4.3     29  0.4
 4.3     29.1    0.7
 4.3     33  0.4
 4.3     35  0.2
 4.3     38  0.9
 4.3     41  0.6
 4.3     42  0.6
 4.3     45  0.1
 4.3     63  0.1
 4.3     71  0.1
 4.4     21  0.9
 4.4     24  0.4
 4.4     29  0.2
 4.4     33  0.6
 4.4     35  0.1
 4.4     42  0
 4.4     43  1
 4.4     45  0.5
 4.4     62  0.1
 4.5     23  0.8
 4.5     29  0.6
 4.5     35  0.3
 4.5     38  1.2
 4.5     39  0.2
 4.5     40  -0.3
 4.5     42  0.6
 4.5     42.1    0.6
 4.6     22  1.1
 4.6     25  0.5
 4.6     25.1    0.6
 4.6     32  0.6
 4.6     34  0.7
 4.6     35  0.6
 4.6     37  0.4
 4.6     41  0.7
 4.7     27  0.7
 4.7     32  0.6
 4.7     33  0.5
 4.7     37  0.3
 4.8     23  0.9
 4.8     25  0.6
 4.8     26  1.1
 4.8     34  0.7
 4.8     34.1    0.6
 4.8     37  0.2
 4.8     38  1.1
 4.8     47  0.7
 4.8     57  0.5
 4.8     64  0.4
 4.9     21  0.9
 4.9     23  1
 4.9     26  0.5
 4.9     27  0.5
 4.9     28  0.3
 4.9     28.1    0.2
 4.9     33  0.7
 4.9     33.1    0.5
 4.9     34  0.5
 4.9     40  0.7
 4.9     44  0.5
 5   30  0.4
 5   41  0.2
 5   48  0.7
 5.1     27  0.5
 5.1     33  0.6
 5.1     43  0.1
 5.1     45  0.5
 5.2     30  0.7
 5.2     31  0.7
 5.2     35  0.6
 5.2     38  1.1
 5.2     47  0.5
 5.3     30  0.5
 5.3     32  0.4
 5.4     30  0.6
 5.4     32  0.3
 5.4     38  0.9
 5.5     32  0.7
 5.5     39  0.9
 5.6     38  0.8
 5.6     41  -0.7
 5.7     21  -0.4
 5.7     33  0.3
 5.7     37  0.9
 5.8     64  0.1
 5.9     34  0.3
 6   26  0.9
 6.5     61  -0.1

The result is as follows:3D Delaunay triangulation with constant colors at each triangle

To allow color inside each Delaunay triangle changes as z changes, I should change one of the last statements, namely ax.plot_trisurf(triang, z, cmap='jet', alpha=1) I tried changing cmap='jet' by cmap=cm.hot(np.sqrt(z**2)) since I had included the cm library at the beginning -by the way, I was forced to write sqrt(z**2) and not just (z) because surprisingly just the function z is not accepted-. Once I tried this, I got the error message if cmap: ^^^^ ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Upvotes: 0

Views: 51

Answers (1)

Here is an example of how to visualize Dalaunay triangulation in matplotlib. You'll need to use mlp_toolkits and some of its modules to do it, as well as scipy:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
from scipy.spatial import Delaunay

x = np.random.rand(100)
y = np.random.rand(100)
z = 0.5*np.sin(x) + 0.2*np.cos(y)

tri = Delaunay(np.array([x, y]).T)

def color_function(z):
    return z  

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

for simplex in tri.simplices:
    x_coords = x[simplex]
    y_coords = y[simplex]
    z_coords = z[simplex]
    
    mean_z = np.mean(z_coords)
    
    color = plt.cm.viridis(color_function(mean_z))  
    verts = [list(zip(x_coords, y_coords, z_coords))]
    ax.add_collection3d(Poly3DCollection(verts, facecolors=[color], edgecolors='k', linewidths=1, alpha=0.9))

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

which gives

enter image description here

Upvotes: 0

Related Questions