@@ -1480,15 +1480,24 @@ def _unconvert_from_RGB_255(colors):
1480
1480
between 0 and 1
1481
1481
1482
1482
"""
1483
- un_rgb_colors = []
1484
- for color in colors :
1485
- un_rgb_color = (color [0 ]/ (255.0 ),
1486
- color [1 ]/ (255.0 ),
1487
- color [2 ]/ (255.0 ))
1483
+ if isinstance (colors , tuple ):
1484
+
1485
+ un_rgb_color = (colors [0 ]/ (255.0 ),
1486
+ colors [1 ]/ (255.0 ),
1487
+ colors [2 ]/ (255.0 ))
1488
+
1489
+ return un_rgb_color
1490
+
1491
+ if isinstance (colors , list ):
1492
+ un_rgb_colors = []
1493
+ for color in colors :
1494
+ un_rgb_color = (color [0 ]/ (255.0 ),
1495
+ color [1 ]/ (255.0 ),
1496
+ color [2 ]/ (255.0 ))
1488
1497
1489
- un_rgb_colors .append (un_rgb_color )
1498
+ un_rgb_colors .append (un_rgb_color )
1490
1499
1491
- return un_rgb_colors
1500
+ return un_rgb_colors
1492
1501
1493
1502
@staticmethod
1494
1503
def _map_z2color (zval , colormap , vmin , vmax ):
@@ -1640,10 +1649,11 @@ def create_trisurf(x, y, z, simplices, colormap=None,
1640
1649
:param (array) simplices: an array of shape (ntri, 3) where ntri is
1641
1650
the number of triangles in the triangularization. Each row of the
1642
1651
array contains the indicies of the verticies of each triangle.
1643
- :param (str|list) colormap: either a plotly scale name, or a list
1644
- containing 2 triplets. These triplets must be of the form (a,b,c)
1645
- or 'rgb(x,y,z)' where a,b,c belong to the interval [0,1] and x,y,z
1646
- belong to [0,255]
1652
+ :param (str|tuple|list) colormap: takes a plotly scale, an rgb or hex
1653
+ string, a tuple, or a list of colors of the aforementioned rgb,
1654
+ hex or tuple types. An rgb/triplet color type is a triplet of the
1655
+ form (a,b,c) or 'rgb(x,y,z)' respectively where a,b,c belong to
1656
+ the interval [0,1] and x,y,z belong to [0,255]
1647
1657
:param (function) dist_func: The function that determines how the
1648
1658
coloring of the surface changes. It takes 3 arguments x, y, z and
1649
1659
must return a formula of these variables which can include numpy
@@ -1794,7 +1804,9 @@ def dist_origin(x, y, z):
1794
1804
1795
1805
# Create a figure
1796
1806
fig1 = FF.create_trisurf(x=x, y=y, z=z,
1797
- colormap="Blues",
1807
+ colormap=['#604d9e',
1808
+ 'rgb(50, 150, 255)',
1809
+ (0.2, 0.2, 0.8)],
1798
1810
simplices=simplices,
1799
1811
dist_func=dist_origin)
1800
1812
# Plot the data
@@ -1832,52 +1844,76 @@ def dist_origin(x, y, z):
1832
1844
colormap = plotly_scales [colormap ]
1833
1845
colormap = FigureFactory ._unlabel_rgb (colormap )
1834
1846
colormap = FigureFactory ._unconvert_from_RGB_255 (colormap )
1835
- else :
1836
- if ('rgb' not in colormap ) and ('#' not in colormap ):
1837
- scale_keys = list (plotly_scales .keys ())
1838
- raise exceptions .PlotlyError ("If you input a string "
1839
- "for 'colormap', it must "
1840
- "either be a Plotly "
1841
- "colorscale, an 'rgb' "
1842
- "color or a hex color."
1843
- "Valud plotly colorscale "
1844
- "names are {}" .format (scale_keys ))
1845
- # put colormap string into a list
1847
+
1848
+ elif 'rgb' in colormap :
1849
+ # put colormap in list
1846
1850
colors_list = []
1847
1851
colors_list .append (colormap )
1848
1852
colormap = colors_list
1853
+
1849
1854
colormap = FigureFactory ._unlabel_rgb (colormap )
1850
1855
colormap = FigureFactory ._unconvert_from_RGB_255 (colormap )
1851
1856
1852
- else :
1853
- if not isinstance (colormap , list ):
1854
- raise exceptions .PlotlyError ("If 'colormap' is a list, then "
1855
- "its items must be tripets of "
1856
- "the form a,b,c or 'rgbx,y,z' "
1857
- "where a,b,c are between 0 and "
1858
- "1 inclusive and x,y,z are "
1859
- "between 0 and 255 inclusive." )
1860
- if 'rgb' in colormap [0 ]:
1861
- colormap = FigureFactory ._unlabel_rgb (colormap )
1862
- for color in colormap :
1863
- for index in range (3 ):
1864
- if color [index ] > 255.0 :
1857
+ elif '#' in colormap :
1858
+ colormap = FigureFactory ._hex_to_rgb (colormap )
1859
+ colormap = FigureFactory ._unconvert_from_RGB_255 (colormap )
1860
+
1861
+ # put colormap in list
1862
+ colors_list = []
1863
+ colors_list .append (colormap )
1864
+ colormap = colors_list
1865
+
1866
+ else :
1867
+ scale_keys = list (plotly_scales .keys ())
1868
+ raise exceptions .PlotlyError ("If you input a string "
1869
+ "for 'colormap', it must "
1870
+ "either be a Plotly "
1871
+ "colorscale, an 'rgb' "
1872
+ "color or a hex color."
1873
+ "Valud plotly colorscale "
1874
+ "names are {}" .format (scale_keys ))
1875
+ elif isinstance (colormap , tuple ):
1876
+ colors_list = []
1877
+ colors_list .append (colormap )
1878
+ colormap = colors_list
1879
+
1880
+ elif isinstance (colormap , list ):
1881
+ new_colormap = []
1882
+ for color in colormap :
1883
+ if 'rgb' in color :
1884
+ color = FigureFactory ._unlabel_rgb (color )
1885
+
1886
+ for value in color :
1887
+ if value > 255.0 :
1865
1888
raise exceptions .PlotlyError ("Whoops! The "
1866
1889
"elements in your "
1867
1890
"rgb colormap "
1868
1891
"tuples cannot "
1869
1892
"exceed 255.0." )
1870
- colormap = FigureFactory ._unconvert_from_RGB_255 (colormap )
1871
1893
1872
- if isinstance (colormap [0 ], tuple ):
1873
- for color in colormap :
1874
- for index in range (3 ):
1875
- if color [index ] > 1.0 :
1894
+ color = FigureFactory ._unconvert_from_RGB_255 (color )
1895
+ new_colormap .append (color )
1896
+ elif '#' in color :
1897
+ color = FigureFactory ._hex_to_rgb (color )
1898
+ color = FigureFactory ._unconvert_from_RGB_255 (color )
1899
+ new_colormap .append (color )
1900
+ elif isinstance (color , tuple ):
1901
+
1902
+ for value in color :
1903
+ if value > 1.0 :
1876
1904
raise exceptions .PlotlyError ("Whoops! The "
1877
- "elements in your "
1878
- "rgb colormap "
1905
+ "elements in "
1906
+ "your colormap "
1879
1907
"tuples cannot "
1880
1908
"exceed 1.0." )
1909
+ new_colormap .append (color )
1910
+ colormap = new_colormap
1911
+
1912
+ else :
1913
+ raise exceptions .PlotlyError ("You must input a valid colormap. "
1914
+ "Valid types include a plotly scale, "
1915
+ "rgb, hex or tuple color, or lastly "
1916
+ "a list of any color types." )
1881
1917
1882
1918
data1 = FigureFactory ._trisurf (x , y , z , simplices ,
1883
1919
dist_func = dist_func ,
@@ -2907,16 +2943,20 @@ def _label_rgb(colors):
2907
2943
"""
2908
2944
Takes colors (a, b, c) and returns tuples 'rgb(a, b, c)'
2909
2945
2910
- Takes a list of two color tuples of the form (a, b, c) and returns the
2911
- same list with each tuple replaced by a string 'rgb(a, b, c)'
2946
+ Takes either a list or a single color tuple of the form (a, b, c) and
2947
+ returns the same colors(s) with each tuple replaced by a string
2948
+ 'rgb(a, b, c)'
2912
2949
2913
2950
"""
2914
- colors_label = []
2915
- for color in colors :
2916
- color_label = 'rgb{}' .format (color )
2917
- colors_label .append (color_label )
2951
+ if isinstance (colors , tuple ):
2952
+ return 'rgb{}' .format (colors )
2953
+ else :
2954
+ colors_label = []
2955
+ for color in colors :
2956
+ color_label = 'rgb{}' .format (color )
2957
+ colors_label .append (color_label )
2918
2958
2919
- return colors_label
2959
+ return colors_label
2920
2960
2921
2961
@staticmethod
2922
2962
def _unlabel_rgb (colors ):
@@ -2929,16 +2969,15 @@ def _unlabel_rgb(colors):
2929
2969
(a, b, c)
2930
2970
2931
2971
"""
2932
- unlabelled_colors = []
2933
- for character in colors :
2972
+ if isinstance (colors , str ):
2934
2973
str_vals = ''
2935
- for index in range (len (character )):
2974
+ for index in range (len (colors )):
2936
2975
try :
2937
- float (character [index ])
2938
- str_vals = str_vals + character [index ]
2976
+ float (colors [index ])
2977
+ str_vals = str_vals + colors [index ]
2939
2978
except ValueError :
2940
- if (character [index ] == ',' ) or (character [index ] == '.' ):
2941
- str_vals = str_vals + character [index ]
2979
+ if (colors [index ] == ',' ) or (colors [index ] == '.' ):
2980
+ str_vals = str_vals + colors [index ]
2942
2981
2943
2982
str_vals = str_vals + ','
2944
2983
numbers = []
@@ -2949,10 +2988,33 @@ def _unlabel_rgb(colors):
2949
2988
else :
2950
2989
numbers .append (float (str_num ))
2951
2990
str_num = ''
2952
- unlabelled_tuple = (numbers [0 ], numbers [1 ], numbers [2 ])
2953
- unlabelled_colors .append (unlabelled_tuple )
2991
+ return (numbers [0 ], numbers [1 ], numbers [2 ])
2992
+
2993
+ if isinstance (colors , list ):
2994
+ unlabelled_colors = []
2995
+ for color in colors :
2996
+ str_vals = ''
2997
+ for index in range (len (color )):
2998
+ try :
2999
+ float (color [index ])
3000
+ str_vals = str_vals + color [index ]
3001
+ except ValueError :
3002
+ if (color [index ] == ',' ) or (color [index ] == '.' ):
3003
+ str_vals = str_vals + color [index ]
3004
+
3005
+ str_vals = str_vals + ','
3006
+ numbers = []
3007
+ str_num = ''
3008
+ for char in str_vals :
3009
+ if char != ',' :
3010
+ str_num = str_num + char
3011
+ else :
3012
+ numbers .append (float (str_num ))
3013
+ str_num = ''
3014
+ unlabelled_tuple = (numbers [0 ], numbers [1 ], numbers [2 ])
3015
+ unlabelled_colors .append (unlabelled_tuple )
2954
3016
2955
- return unlabelled_colors
3017
+ return unlabelled_colors
2956
3018
2957
3019
@staticmethod
2958
3020
def create_scatterplotmatrix (df , dataframe = None , headers = None ,
0 commit comments