@@ -1503,11 +1503,11 @@ def _unconvert_from_RGB_255(colors):
1503
1503
return un_rgb_colors
1504
1504
1505
1505
@staticmethod
1506
- def _map_z2color ( zvals , colormap , vmin , vmax ):
1506
+ def _map_array2color ( array , colormap , vmin , vmax ):
1507
1507
"""
1508
- Returns the color corresponding zval's place between vmin and vmax
1508
+ Normalize values in array by vmin/vmax and return plotly color strings.
1509
1509
1510
- This function takes a z value (zval) along with a colormap and a
1510
+ This function takes an array of values along with a colormap and a
1511
1511
minimum (vmin) and maximum (vmax) range of possible z values for the
1512
1512
given parametrized surface. It returns an rgb color based on the
1513
1513
relative position of zval between vmin and vmax
@@ -1520,7 +1520,7 @@ def _map_z2color(zvals, colormap, vmin, vmax):
1520
1520
"of vmax." )
1521
1521
# find distance t of zval from vmin to vmax where the distance
1522
1522
# is normalized to be between 0 and 1
1523
- t = (zvals - vmin ) / float ((vmax - vmin ))
1523
+ t = (array - vmin ) / float ((vmax - vmin ))
1524
1524
t_colors = FigureFactory ._find_intermediate_color (colormap [0 ],
1525
1525
colormap [1 ],
1526
1526
t )
@@ -1542,34 +1542,46 @@ def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
1542
1542
import numpy as np
1543
1543
from plotly .graph_objs import graph_objs
1544
1544
points3D = np .vstack ((x , y , z )).T
1545
+ simplices = np .atleast_2d (simplices )
1545
1546
1546
1547
# vertices of the surface triangles
1547
1548
tri_vertices = points3D [simplices ]
1548
1549
1549
- if not color_func :
1550
+ # Define colors for the triangle faces
1551
+ if color_func is None :
1550
1552
# mean values of z-coordinates of triangle vertices
1551
1553
mean_dists = tri_vertices [:, :, 2 ].mean (- 1 )
1554
+ elif isinstance (color_func , (list , np .ndarray )):
1555
+ # Pre-computed list / array of values to map onto color
1556
+ if len (color_func ) != len (simplices ):
1557
+ raise ValueError ("If color_func is a list/array, it must "
1558
+ "be the same length as simplices." )
1559
+ mean_dists = np .asarray (color_func )
1552
1560
else :
1553
1561
# apply user inputted function to calculate
1554
1562
# custom coloring for triangle vertices
1555
1563
mean_dists = []
1556
-
1557
1564
for triangle in tri_vertices :
1558
1565
dists = []
1559
1566
for vertex in triangle :
1560
1567
dist = color_func (vertex [0 ], vertex [1 ], vertex [2 ])
1561
1568
dists .append (dist )
1562
-
1563
1569
mean_dists .append (np .mean (dists ))
1570
+ mean_dists = np .asarray (mean_dists )
1564
1571
1565
- min_mean_dists = np .min (mean_dists )
1566
- max_mean_dists = np .max (mean_dists )
1567
- facecolor = FigureFactory ._map_z2color (mean_dists ,
1568
- colormap ,
1569
- min_mean_dists ,
1570
- max_mean_dists )
1571
-
1572
- ii , jj , kk = zip (* simplices )
1572
+ # Check if facecolors are already strings and can be skipped
1573
+ if isinstance (mean_dists [0 ], str ):
1574
+ facecolor = mean_dists
1575
+ else :
1576
+ min_mean_dists = np .min (mean_dists )
1577
+ max_mean_dists = np .max (mean_dists )
1578
+ facecolor = FigureFactory ._map_array2color (mean_dists ,
1579
+ colormap ,
1580
+ min_mean_dists ,
1581
+ max_mean_dists )
1582
+ # Make sure we have arrays to speed up plotting
1583
+ facecolor = np .asarray (facecolor )
1584
+ ii , jj , kk = simplices .T
1573
1585
triangles = graph_objs .Mesh3d (x = x , y = y , z = z , facecolor = facecolor ,
1574
1586
i = ii , j = jj , k = kk , name = '' )
1575
1587
@@ -1634,30 +1646,30 @@ def create_trisurf(x, y, z, simplices, colormap=None, color_func=None,
1634
1646
:param (array) z: data values of z in a 1D array
1635
1647
:param (array) simplices: an array of shape (ntri, 3) where ntri is
1636
1648
the number of triangles in the triangularization. Each row of the
1637
- array contains the indicies of the verticies of each triangle.
1649
+ array contains the indicies of the verticies of each triangle
1638
1650
:param (str|list) colormap: either a plotly scale name, or a list
1639
1651
containing 2 triplets. These triplets must be of the form (a,b,c)
1640
1652
or 'rgb(x,y,z)' where a,b,c belong to the interval [0,1] and x,y,z
1641
1653
belong to [0,255]
1642
1654
:param (function|list) color_func: The parameter that determines the
1643
1655
coloring of the surface. Takes either a function with 3 arguments
1644
1656
x, y, z or a list/array of color values the same length as
1645
- simplices. If set to None, color will only depend on the z axis.
1657
+ simplices. If set to None, color will only depend on the z axis
1646
1658
:param (str) title: title of the plot
1647
1659
:param (bool) plot_edges: determines if the triangles on the trisurf
1648
1660
are visible
1649
1661
:param (bool) showbackground: makes background in plot visible
1650
1662
:param (str) backgroundcolor: color of background. Takes a string of
1651
- the form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive.
1663
+ the form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive
1652
1664
:param (str) gridcolor: color of the gridlines besides the axes. Takes
1653
1665
a string of the form 'rgb(x,y,z)' x,y,z are between 0 and 255
1654
- inclusive.
1666
+ inclusive
1655
1667
:param (str) zerolinecolor: color of the axes. Takes a string of the
1656
- form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive.
1668
+ form 'rgb(x,y,z)' x,y,z are between 0 and 255 inclusive
1657
1669
:param (int|float) height: the height of the plot (in pixels)
1658
1670
:param (int|float) width: the width of the plot (in pixels)
1659
1671
:param (dict) aspectratio: a dictionary of the aspect ratio values for
1660
- the x, y and z axes. 'x', 'y' and 'z' take (int|float) values.
1672
+ the x, y and z axes. 'x', 'y' and 'z' take (int|float) values
1661
1673
1662
1674
Example 1: Sphere
1663
1675
```
0 commit comments