Skip to content

Commit 892d0c8

Browse files
committed
Added hex compatibility to color_func in trisurf and added extra example to the string doc
1 parent bef62ef commit 892d0c8

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

plotly/tools.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,12 @@ def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
15561556
if len(color_func) != len(simplices):
15571557
raise ValueError("If color_func is a list/array, it must "
15581558
"be the same length as simplices.")
1559+
# convert all colors to rgb
1560+
for index in range(len(color_func)):
1561+
if '#' in color_func[index]:
1562+
foo = FigureFactory._hex_to_rgb(color_func[index])
1563+
color_func[index] = FigureFactory._label_rgb(foo)
1564+
15591565
mean_dists = np.asarray(color_func)
15601566
else:
15611567
# apply user inputted function to calculate
@@ -1810,6 +1816,48 @@ def dist_origin(x, y, z):
18101816
# Plot the data
18111817
py.iplot(fig1, filename='Trisurf Plot - Custom Coloring')
18121818
```
1819+
1820+
Example 5: Enter color_func as a list of colors
1821+
```
1822+
# Necessary Imports for Trisurf
1823+
import numpy as np
1824+
from scipy.spatial import Delaunay
1825+
import random
1826+
1827+
import plotly.plotly as py
1828+
from plotly.tools import FigureFactory as FF
1829+
from plotly.graph_objs import graph_objs
1830+
1831+
# Make data for plot
1832+
u=np.linspace(-np.pi, np.pi, 30)
1833+
v=np.linspace(-np.pi, np.pi, 30)
1834+
u,v=np.meshgrid(u,v)
1835+
u=u.flatten()
1836+
v=v.flatten()
1837+
1838+
x = u
1839+
y = u*np.cos(v)
1840+
z = u*np.sin(v)
1841+
1842+
points2D = np.vstack([u,v]).T
1843+
tri = Delaunay(points2D)
1844+
simplices = tri.simplices
1845+
1846+
1847+
colors = []
1848+
color_choices = ['rgb(0, 0, 0)', '#6c4774', '#d6c7dd']
1849+
1850+
for index in range(len(simplices)):
1851+
colors.append(random.choice(color_choices))
1852+
1853+
fig = FF.create_trisurf(
1854+
x, y, z, simplices,
1855+
color_func=colors,
1856+
title=' Modern Art'
1857+
)
1858+
1859+
py.iplot(fig, filename="Modern Art")
1860+
```
18131861
"""
18141862
from plotly.graph_objs import graph_objs
18151863
plotly_scales = {'Greys': ['rgb(0,0,0)', 'rgb(255,255,255)'],

0 commit comments

Comments
 (0)