Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 581f8ba

Browse files
committed
Added custom coloring option
1 parent 8904508 commit 581f8ba

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

plotly/tools.py

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,7 @@ def _tri_indices(simplices):
15591559
return ([triplet[c] for triplet in simplices] for c in range(3))
15601560

15611561
@staticmethod
1562-
def _trisurf(x, y, z, simplices, colormap=None,
1562+
def _trisurf(x, y, z, simplices, colormap=None, dist_func=None,
15631563
plot_edges=None, x_edge=None, y_edge=None, z_edge=None):
15641564
"""
15651565
Refer to FigureFactory.create_trisurf() for docstring
@@ -1574,12 +1574,27 @@ def _trisurf(x, y, z, simplices, colormap=None,
15741574

15751575
# vertices of the surface triangles
15761576
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])
15831598
ii, jj, kk = FigureFactory._tri_indices(simplices)
15841599

15851600
triangles = graph_objs.Mesh3d(x=x, y=y, z=z, facecolor=facecolor,
@@ -1622,7 +1637,7 @@ def _trisurf(x, y, z, simplices, colormap=None,
16221637

16231638
@staticmethod
16241639
def create_trisurf(x, y, z, simplices, colormap=None,
1625-
title='Trisurf Plot',
1640+
dist_func=None, title='Trisurf Plot',
16261641
showbackground=True,
16271642
backgroundcolor='rgb(230, 230, 230)',
16281643
gridcolor='rgb(255, 255, 255)',
@@ -1642,6 +1657,11 @@ def create_trisurf(x, y, z, simplices, colormap=None,
16421657
containing 2 triplets. These triplets must be of the form (a,b,c)
16431658
or 'rgb(x,y,z)' where a,b,c belong to the interval [0,1] and x,y,z
16441659
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.
16451665
:param (str) title: title of the plot
16461666
:param (bool) showbackground: makes background in plot visible
16471667
:param (str) backgroundcolor: color of background. Takes a string of
@@ -1755,6 +1775,44 @@ def create_trisurf(x, y, z, simplices, colormap=None,
17551775
# Plot the data
17561776
py.iplot(fig1, filename='Trisurf Plot - Mobius Band')
17571777
```
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+
```
17581816
"""
17591817
from plotly.graph_objs import graph_objs
17601818
plotly_scales = {'Greys': ['rgb(0,0,0)', 'rgb(255,255,255)'],
@@ -1826,6 +1884,7 @@ def create_trisurf(x, y, z, simplices, colormap=None,
18261884
"exceed 1.0.")
18271885

18281886
data1 = FigureFactory._trisurf(x, y, z, simplices,
1887+
dist_func=dist_func,
18291888
colormap=colormap,
18301889
plot_edges=True)
18311890
axis = dict(

0 commit comments

Comments
 (0)