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