@@ -1490,11 +1490,11 @@ def _unconvert_from_RGB_255(colors):
1490
1490
return un_rgb_colors
1491
1491
1492
1492
@staticmethod
1493
- def _map_z2color ( zvals , colormap , vmin , vmax ):
1493
+ def _map_array2color ( array , colormap , vmin , vmax ):
1494
1494
"""
1495
- Returns the color corresponding zval's place between vmin and vmax
1495
+ Normalize values in array by vmin/vmax and return plotly color strings.
1496
1496
1497
- This function takes a z value (zval) along with a colormap and a
1497
+ This function takes an array of values along with a colormap and a
1498
1498
minimum (vmin) and maximum (vmax) range of possible z values for the
1499
1499
given parametrized surface. It returns an rgb color based on the
1500
1500
relative position of zval between vmin and vmax
@@ -1507,7 +1507,7 @@ def _map_z2color(zvals, colormap, vmin, vmax):
1507
1507
"of vmax." )
1508
1508
# find distance t of zval from vmin to vmax where the distance
1509
1509
# is normalized to be between 0 and 1
1510
- t = (zvals - vmin ) / float ((vmax - vmin ))
1510
+ t = (array - vmin ) / float ((vmax - vmin ))
1511
1511
t_colors = FigureFactory ._find_intermediate_color (colormap [0 ],
1512
1512
colormap [1 ],
1513
1513
t )
@@ -1529,41 +1529,46 @@ def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
1529
1529
import numpy as np
1530
1530
from plotly .graph_objs import graph_objs
1531
1531
points3D = np .vstack ((x , y , z )).T
1532
+ simplices = np .atleast_2d (simplices )
1532
1533
1533
1534
# vertices of the surface triangles
1534
1535
tri_vertices = points3D [simplices ]
1535
1536
1537
+ # Define colors for the triangle faces
1536
1538
if color_func is None :
1537
1539
# mean values of z-coordinates of triangle vertices
1538
1540
mean_dists = tri_vertices [:, :, 2 ].mean (- 1 )
1539
1541
elif isinstance (color_func , (list , np .ndarray )):
1542
+ # Pre-computed list / array of values to map onto color
1540
1543
if len (color_func ) != len (simplices ):
1541
1544
raise ValueError ('If color_func is a list/array, must'
1542
1545
' be the same length as simplices' )
1543
- mean_dists = color_func
1546
+ mean_dists = np . asarray ( color_func )
1544
1547
else :
1545
1548
# apply user inputted function to calculate
1546
1549
# custom coloring for triangle vertices
1547
1550
mean_dists = []
1548
-
1549
1551
for triangle in tri_vertices :
1550
1552
dists = []
1551
1553
for vertex in triangle :
1552
1554
dist = color_func (vertex [0 ], vertex [1 ], vertex [2 ])
1553
1555
dists .append (dist )
1554
-
1555
1556
mean_dists .append (np .mean (dists ))
1557
+ mean_dists = np .asarray (mean_dists )
1558
+
1559
+ # Check if facecolors are already strings and can be skipped
1556
1560
if isinstance (mean_dists [0 ], str ):
1557
1561
facecolor = mean_dists
1558
1562
else :
1559
1563
min_mean_dists = np .min (mean_dists )
1560
1564
max_mean_dists = np .max (mean_dists )
1561
- facecolor = FigureFactory ._map_z2color (mean_dists ,
1562
- colormap ,
1563
- min_mean_dists ,
1564
- max_mean_dists )
1565
-
1566
- ii , jj , kk = zip (* simplices )
1565
+ facecolor = FigureFactory ._map_array2color (mean_dists ,
1566
+ colormap ,
1567
+ min_mean_dists ,
1568
+ max_mean_dists )
1569
+ # Make sure we have arrays to speed up plotting
1570
+ facecolor = np .asarray (facecolor )
1571
+ ii , jj , kk = simplices .T
1567
1572
triangles = graph_objs .Mesh3d (x = x , y = y , z = z , facecolor = facecolor ,
1568
1573
i = ii , j = jj , k = kk , name = '' )
1569
1574
0 commit comments