Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a328f86

Browse filesBrowse files
committed
Finally added colorbar visibilitiy
1 parent 29ea770 commit a328f86
Copy full SHA for a328f86

File tree

Expand file treeCollapse file tree

1 file changed

+58
-32
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+58
-32
lines changed

‎plotly/tools.py

Copy file name to clipboardExpand all lines: plotly/tools.py
+58-32Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
'Blackbody': ['rgb(0,0,0)', 'rgb(160,200,255)'],
4747
'Earth': ['rgb(0,0,130)', 'rgb(255,255,255)'],
4848
'Electric': ['rgb(0,0,0)', 'rgb(255,250,220)'],
49-
'Viridis': 'Viridis'}
50-
#['rgb(68,1,84)', 'rgb(253,231,37)']}
49+
'Viridis': ['rgb(68,1,84)', 'rgb(253,231,37)']}
5150

5251
# color constants for violin plot
5352
DEFAULT_FILLCOLOR = '#1f77b4'
@@ -1478,18 +1477,42 @@ class FigureFactory(object):
14781477
"""
14791478

14801479
@staticmethod
1481-
def _make_colorscale(colors):
1480+
def _make_colorscale(colors, scale=None):
14821481
"""
1483-
Makes a list of colors into a colorscale-acceptable form
1482+
Makes a colorscale from a list of colors and scale
14841483
1484+
Takes a list of colors and scales and constructs a colorscale based
1485+
on the colors in sequential order. If 'scale' is left empty, a linear-
1486+
interpolated colorscale will be generated. If 'scale' is a specificed
1487+
list, it must be the same legnth as colors and must contain all floats
14851488
For documentation regarding to the form of the output, see
14861489
https://plot.ly/python/reference/#mesh3d-colorscale
14871490
"""
14881491
colorscale = []
1489-
diff = 1./(len(colors) - 1)
14901492

1491-
for j in range(len(colors)):
1492-
colorscale.append([j * diff, colors[j]])
1493+
if not scale:
1494+
for j in range(len(colors)):
1495+
colorscale.append([j * 1./(len(colors) - 1), colors[j]])
1496+
return colorscale
1497+
1498+
else:
1499+
colorscale = [[scale[j], colors[j]] for j in range(len(colors))]
1500+
return colorscale
1501+
1502+
@staticmethod
1503+
def _convert_colorscale_to_rgb(colorscale):
1504+
"""
1505+
Converts the colors in a colorscale to rgb colors
1506+
"""
1507+
for index in range(len(colorscale)):
1508+
colorscale[index][1] = FigureFactory._convert_to_RGB_255(
1509+
colorscale[index][1]
1510+
)
1511+
1512+
for index in range(len(colorscale)):
1513+
colorscale[index][1] = FigureFactory._label_rgb(
1514+
colorscale[index][1]
1515+
)
14931516
return colorscale
14941517

14951518
@staticmethod
@@ -2996,7 +3019,7 @@ def _find_intermediate_color(lowcolor, highcolor, intermed):
29963019
@staticmethod
29973020
def _color_parser(colors, function):
29983021
"""
2999-
Takes color(s) and a function and applys the function on the color(s)
3022+
Takes color(s) and a function and applies the function on the color(s)
30003023
30013024
In particular, this function identifies whether the given color object
30023025
is an iterable or not and applies the given color-parsing function to
@@ -3081,9 +3104,9 @@ def _map_face2color(face, colormap, vmin, vmax):
30813104
return face_color
30823105

30833106
@staticmethod
3084-
def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
3085-
plot_edges=False, x_edge=None, y_edge=None, z_edge=None,
3086-
facecolor=None):
3107+
def _trisurf(x, y, z, simplices, show_colorbar, colormap=None,
3108+
color_func=None, plot_edges=False, x_edge=None, y_edge=None,
3109+
z_edge=None, facecolor=None):
30873110
"""
30883111
Refer to FigureFactory.create_trisurf() for docstring
30893112
"""
@@ -3149,26 +3172,25 @@ def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
31493172
facecolor = np.asarray(facecolor)
31503173
ii, jj, kk = simplices.T
31513174

3152-
# Adding intensity to Mesh3D Data
3153-
intensity = [0]
3175+
# make a colorscale from the colors
3176+
colorscale = FigureFactory._make_colorscale(colormap)
3177+
colorscale = FigureFactory._convert_colorscale_to_rgb(colorscale)
31543178

3155-
div = 1./(len(x) - 1)
3156-
3157-
for j in range(1, len(x)+1):
3158-
intensity.append(j * div)
3159-
#print intensity
3160-
#intensity = [0 for j in range(len(x))]
3179+
triangles = graph_objs.Mesh3d(x=x, y=y, z=z, facecolor=facecolor,
3180+
i=ii, j=jj, k=kk, name='')
31613181

3162-
colorscale_numbered = FigureFactory._make_colorscale(colormap)
3182+
colorbar = graph_objs.Mesh3d(x=[0, 1], y=[0, 1], z=[0, 1],
3183+
colorscale=colorscale,
3184+
intensity=[0, 1],
3185+
showscale=True)
31633186

3164-
triangles = graph_objs.Mesh3d(x=x, y=y, z=z, facecolor=facecolor,
3165-
i=ii, j=jj, k=kk, name='',
3166-
intensity=intensity,
3167-
colorscale=colorscale_numbered,
3168-
showscale=True)
3187+
# the triangle sides are not plotted
3188+
if plot_edges is not True:
3189+
if show_colorbar is True:
3190+
return graph_objs.Data([triangles, colorbar])
3191+
else:
3192+
return graph_objs.Data([triangles])
31693193

3170-
if plot_edges is not True: # the triangle sides are not plotted
3171-
return graph_objs.Data([triangles])
31723194

31733195
# define the lists x_edge, y_edge and z_edge, of x, y, resp z
31743196
# coordinates of edge end points for each triangle
@@ -3208,12 +3230,14 @@ def _trisurf(x, y, z, simplices, colormap=None, color_func=None,
32083230
line=graph_objs.Line(color='rgb(50, 50, 50)',
32093231
width=1.5)
32103232
)
3211-
3212-
return graph_objs.Data([triangles, lines])
3233+
if show_colorbar is True:
3234+
return graph_objs.Data([triangles, lines, colorbar])
3235+
else:
3236+
return graph_objs.Data([triangles, lines])
32133237

32143238
@staticmethod
3215-
def create_trisurf(x, y, z, simplices, colormap=None, color_func=None,
3216-
title='Trisurf Plot', plot_edges=True,
3239+
def create_trisurf(x, y, z, simplices, colormap=None, show_colorbar=True,
3240+
color_func=None, title='Trisurf Plot', plot_edges=True,
32173241
showbackground=True,
32183242
backgroundcolor='rgb(230, 230, 230)',
32193243
gridcolor='rgb(255, 255, 255)',
@@ -3234,7 +3258,8 @@ def create_trisurf(x, y, z, simplices, colormap=None, color_func=None,
32343258
of the form 'rgb(x, y, z)' where x, y, z belong to the interval
32353259
[0, 255] and a color tuple is a tuple of the form (a, b, c) where
32363260
a, b and c belong to [0, 1]. If colormap is a list, it must
3237-
contain the valid color types aforementioned as its members.
3261+
contain the valid color types aforementioned as its members
3262+
:param (bool) show_colorbar: determines if colorbar is visible
32383263
:param (function|list) color_func: The parameter that determines the
32393264
coloring of the surface. Takes either a function with 3 arguments
32403265
x, y, z or a list/array of color values the same length as
@@ -3443,6 +3468,7 @@ def dist_origin(x, y, z):
34433468
colormap = FigureFactory._validate_colors(colormap, 'tuple')
34443469

34453470
data1 = FigureFactory._trisurf(x, y, z, simplices,
3471+
show_colorbar=show_colorbar,
34463472
color_func=color_func,
34473473
colormap=colormap,
34483474
plot_edges=plot_edges)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.