Skip to content

Commit f147b62

Browse files
committed
Allow more than 2 colormaps again
1 parent 892d0c8 commit f147b62

File tree

1 file changed

+57
-31
lines changed

1 file changed

+57
-31
lines changed

plotly/tools.py

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,9 +1468,9 @@ def _find_intermediate_color(lowcolor, highcolor, intermed):
14681468
diff_1 = float(highcolor[1] - lowcolor[1])
14691469
diff_2 = float(highcolor[2] - lowcolor[2])
14701470

1471-
inter_colors = np.array([lowcolor[0] + intermed * diff_0,
1472-
lowcolor[1] + intermed * diff_1,
1473-
lowcolor[2] + intermed * diff_2])
1471+
inter_colors = (lowcolor[0] + intermed * diff_0,
1472+
lowcolor[1] + intermed * diff_1,
1473+
lowcolor[2] + intermed * diff_2)
14741474
return inter_colors
14751475

14761476
@staticmethod
@@ -1503,35 +1503,53 @@ def _unconvert_from_RGB_255(colors):
15031503
return un_rgb_colors
15041504

15051505
@staticmethod
1506-
def _map_array2color(array, colormap, vmin, vmax):
1506+
def _map_face2color(face, colormap, vmin, vmax):
15071507
"""
1508-
Normalize values in array by vmin/vmax and return plotly color strings.
1508+
Normalize facecolor values by vmin/vmax and return rgb-color strings
15091509
1510-
This function takes an array of values along with a colormap and a
1511-
minimum (vmin) and maximum (vmax) range of possible z values for the
1512-
given parametrized surface. It returns an rgb color based on the
1513-
relative position of zval between vmin and vmax
1510+
This function takes a tuple color with elements between 0 and 1, along
1511+
with a colormap and a minimum (vmin) and maximum (vmax) range of
1512+
possible mean distances for the given parametrized surface. It returns
1513+
an rgb color based on the mean distance between vmin and vmax
15141514
15151515
"""
15161516
if vmin >= vmax:
15171517
raise exceptions.PlotlyError("Incorrect relation between vmin "
15181518
"and vmax. The vmin value cannot be "
15191519
"bigger than or equal to the value "
15201520
"of vmax.")
1521-
# find distance t of zval from vmin to vmax where the distance
1522-
# is normalized to be between 0 and 1
1523-
t = (array - vmin) / float((vmax - vmin))
1524-
t_colors = FigureFactory._find_intermediate_color(colormap[0],
1525-
colormap[1],
1526-
t)
1527-
t_colors = t_colors * 255.
1528-
labelled_colors = ['rgb(%s, %s, %s)' % (i, j, k)
1529-
for i, j, k in t_colors.T]
1530-
return labelled_colors
1521+
# find the normalized distance t of a triangle face between vmin and
1522+
#vmax where the distance is normalized between 0 and 1
1523+
t = (face - vmin) / float((vmax - vmin))
1524+
1525+
if len(colormap) <= 1:
1526+
t_color = colormap[0]
1527+
color = FigureFactory._convert_to_RGB_255(t_color)
1528+
color = FigureFactory._label_rgb(color)
1529+
else:
1530+
# account for colormaps with more than one color
1531+
incr = 1./(len(colormap) - 1)
1532+
low_color_index = int(t/incr)
1533+
1534+
if t == 1:
1535+
t_color = colormap[low_color_index]
1536+
color = FigureFactory._convert_to_RGB_255(t_color)
1537+
color = FigureFactory._label_rgb(color)
1538+
1539+
else:
1540+
t_color = FigureFactory._find_intermediate_color(
1541+
colormap[low_color_index],
1542+
colormap[low_color_index + 1],
1543+
(t - low_color_index * incr) / incr)
1544+
color = FigureFactory._convert_to_RGB_255(t_color)
1545+
color = FigureFactory._label_rgb(color)
1546+
1547+
return color
15311548

15321549
@staticmethod
15331550
def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
1534-
plot_edges=False, x_edge=None, y_edge=None, z_edge=None):
1551+
plot_edges=False, x_edge=None, y_edge=None, z_edge=None,
1552+
facecolor=None):
15351553
"""
15361554
Refer to FigureFactory.create_trisurf() for docstring
15371555
"""
@@ -1556,11 +1574,11 @@ def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
15561574
if len(color_func) != len(simplices):
15571575
raise ValueError("If color_func is a list/array, it must "
15581576
"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)
1577+
# convert all colors to rgb
1578+
for index in range(len(color_func)):
1579+
if '#' in color_func[index]:
1580+
foo = FigureFactory._hex_to_rgb(color_func[index])
1581+
color_func[index] = FigureFactory._label_rgb(foo)
15641582

15651583
mean_dists = np.asarray(color_func)
15661584
else:
@@ -1581,10 +1599,16 @@ def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
15811599
else:
15821600
min_mean_dists = np.min(mean_dists)
15831601
max_mean_dists = np.max(mean_dists)
1584-
facecolor = FigureFactory._map_array2color(mean_dists,
1585-
colormap,
1586-
min_mean_dists,
1587-
max_mean_dists)
1602+
1603+
if facecolor is None:
1604+
facecolor = []
1605+
for index in range(len(mean_dists)):
1606+
color = FigureFactory._map_face2color(mean_dists[index],
1607+
colormap,
1608+
min_mean_dists,
1609+
max_mean_dists)
1610+
facecolor.append(color)
1611+
15881612
# Make sure we have arrays to speed up plotting
15891613
facecolor = np.asarray(facecolor)
15901614
ii, jj, kk = simplices.T
@@ -2931,11 +2955,13 @@ def _label_rgb(colors):
29312955
29322956
"""
29332957
if isinstance(colors, tuple):
2934-
return 'rgb{}'.format(colors)
2958+
return ('rgb(%s, %s, %s)' % (colors[0], colors[1], colors[2]))
29352959
else:
29362960
colors_label = []
29372961
for color in colors:
2938-
color_label = 'rgb{}'.format(color)
2962+
color_label = ('rgb(%s, %s, %s)' % (color[0],
2963+
color[1],
2964+
color[2]))
29392965
colors_label.append(color_label)
29402966

29412967
return colors_label

0 commit comments

Comments
 (0)