@@ -1559,7 +1559,7 @@ def _tri_indices(simplices):
1559
1559
return ([triplet [c ] for triplet in simplices ] for c in range (3 ))
1560
1560
1561
1561
@staticmethod
1562
- def _trisurf (x , y , z , simplices , colormap = None ,
1562
+ def _trisurf (x , y , z , simplices , colormap = None , dist_func = None ,
1563
1563
plot_edges = None , x_edge = None , y_edge = None , z_edge = None ):
1564
1564
"""
1565
1565
Refer to FigureFactory.create_trisurf() for docstring
@@ -1574,12 +1574,27 @@ def _trisurf(x, y, z, simplices, colormap=None,
1574
1574
1575
1575
# vertices of the surface triangles
1576
1576
tri_vertices = list (map (lambda index : points3D [index ], simplices ))
1577
- # mean values of z-coordinates of triangle vertices
1578
- zmean = [np .mean (tri [:, 2 ]) for tri in tri_vertices ]
1579
- min_zmean = np .min (zmean )
1580
- max_zmean = np .max (zmean )
1581
- facecolor = ([FigureFactory ._map_z2color (zz , colormap , min_zmean ,
1582
- max_zmean ) for zz in zmean ])
1577
+
1578
+ if not dist_func :
1579
+ # mean values of z-coordinates of triangle vertices
1580
+ mean_dists = [np .mean (tri [:, 2 ]) for tri in tri_vertices ]
1581
+ else :
1582
+ # apply user inputted function to calculate
1583
+ # custom coloring for triangle vertices
1584
+ mean_dists = []
1585
+
1586
+ for triangle in tri_vertices :
1587
+ dists = []
1588
+ for vertex in triangle :
1589
+ dist = dist_func (vertex [0 ], vertex [1 ], vertex [2 ])
1590
+ dists .append (dist )
1591
+
1592
+ mean_dists .append (np .mean (dists ))
1593
+
1594
+ min_mean_dists = np .min (mean_dists )
1595
+ max_mean_dists = np .max (mean_dists )
1596
+ facecolor = ([FigureFactory ._map_z2color (zz , colormap , min_mean_dists ,
1597
+ max_mean_dists ) for zz in mean_dists ])
1583
1598
ii , jj , kk = FigureFactory ._tri_indices (simplices )
1584
1599
1585
1600
triangles = graph_objs .Mesh3d (x = x , y = y , z = z , facecolor = facecolor ,
@@ -1622,7 +1637,7 @@ def _trisurf(x, y, z, simplices, colormap=None,
1622
1637
1623
1638
@staticmethod
1624
1639
def create_trisurf (x , y , z , simplices , colormap = None ,
1625
- title = 'Trisurf Plot' ,
1640
+ dist_func = None , title = 'Trisurf Plot' ,
1626
1641
showbackground = True ,
1627
1642
backgroundcolor = 'rgb(230, 230, 230)' ,
1628
1643
gridcolor = 'rgb(255, 255, 255)' ,
@@ -1642,6 +1657,11 @@ def create_trisurf(x, y, z, simplices, colormap=None,
1642
1657
containing 2 triplets. These triplets must be of the form (a,b,c)
1643
1658
or 'rgb(x,y,z)' where a,b,c belong to the interval [0,1] and x,y,z
1644
1659
belong to [0,255]
1660
+ :param (function) dist_func: The function that determines how the
1661
+ coloring of the surface changes. It takes 3 arguments x, y, z and
1662
+ must return a formula of these variables which can include numpy
1663
+ functions (eg. np.sqrt). If set to None, color will only depend on
1664
+ the z axis.
1645
1665
:param (str) title: title of the plot
1646
1666
:param (bool) showbackground: makes background in plot visible
1647
1667
:param (str) backgroundcolor: color of background. Takes a string of
@@ -1755,6 +1775,44 @@ def create_trisurf(x, y, z, simplices, colormap=None,
1755
1775
# Plot the data
1756
1776
py.iplot(fig1, filename='Trisurf Plot - Mobius Band')
1757
1777
```
1778
+
1779
+ Example 4: Using a Custom Colormap Function with Light Cone
1780
+ ```
1781
+ # Necessary Imports for Trisurf
1782
+ import numpy as np
1783
+ from scipy.spatial import Delaunay
1784
+
1785
+ import plotly.plotly as py
1786
+ from plotly.tools import FigureFactory as FF
1787
+ from plotly.graph_objs import graph_objs
1788
+
1789
+ # Make data for plot
1790
+ u=np.linspace(-np.pi, np.pi, 30)
1791
+ v=np.linspace(-np.pi, np.pi, 30)
1792
+ u,v=np.meshgrid(u,v)
1793
+ u=u.flatten()
1794
+ v=v.flatten()
1795
+
1796
+ x = u
1797
+ y = u*np.cos(v)
1798
+ z = u*np.sin(v)
1799
+
1800
+ points2D = np.vstack([u,v]).T
1801
+ tri = Delaunay(points2D)
1802
+ simplices = tri.simplices
1803
+
1804
+ # Define distance function
1805
+ def dist_origin(x, y, z):
1806
+ return np.sqrt((1.0 * x)**2 + (1.0 * y)**2 + (1.0 * z)**2)
1807
+
1808
+ # Create a figure
1809
+ fig1 = FF.create_trisurf(x=x, y=y, z=z,
1810
+ colormap="Blues",
1811
+ simplices=simplices,
1812
+ dist_func=dist_origin)
1813
+ # Plot the data
1814
+ py.iplot(fig1, filename='Trisurf Plot - Custom Coloring')
1815
+ ```
1758
1816
"""
1759
1817
from plotly .graph_objs import graph_objs
1760
1818
plotly_scales = {'Greys' : ['rgb(0,0,0)' , 'rgb(255,255,255)' ],
@@ -1826,6 +1884,7 @@ def create_trisurf(x, y, z, simplices, colormap=None,
1826
1884
"exceed 1.0." )
1827
1885
1828
1886
data1 = FigureFactory ._trisurf (x , y , z , simplices ,
1887
+ dist_func = dist_func ,
1829
1888
colormap = colormap ,
1830
1889
plot_edges = True )
1831
1890
axis = dict (
0 commit comments