Skip to content

Commit 11ad62c

Browse files
authored
Merge pull request #547 from plotly/fix-trisurf-color-GOOD
force rgb colors to be a triplet of integers, not floats
2 parents 1f38da0 + 553ac98 commit 11ad62c

File tree

5 files changed

+116
-87
lines changed

5 files changed

+116
-87
lines changed

plotly/tests/test_core/test_tools/test_figure_factory.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,32 +1402,32 @@ def test_2D_density_all_args(self):
14021402
point_size=3, height=800, width=800)
14031403

14041404
exp_2D_density_chart = {
1405-
'data': [{'marker': {'color': 'rgb(0.0, 0.0, 127.5)',
1405+
'data': [{'marker': {'color': 'rgb(0, 0, 128)',
14061406
'opacity': 0.4,
14071407
'size': 3},
14081408
'mode': 'markers',
14091409
'name': 'points',
14101410
'type': 'scatter',
14111411
'x': [1, 2],
14121412
'y': [2, 4]},
1413-
{'colorscale': [[0.0, 'rgb(122.0, 69.0, 121.0)'],
1414-
[0.25, 'rgb(213.0, 96.0, 115.0)'],
1415-
[0.5, 'rgb(236.0, 158.0, 105.0)'],
1416-
[0.75, 'rgb(255.0, 255.0, 51.0)'],
1417-
[1.0, 'rgb(249.9, 249.9, 249.9)']],
1413+
{'colorscale': [[0.0, 'rgb(122, 69, 121)'],
1414+
[0.25, 'rgb(213, 96, 115)'],
1415+
[0.5, 'rgb(236, 158, 105)'],
1416+
[0.75, 'rgb(255, 255, 51)'],
1417+
[1.0, 'rgb(250, 250, 250)']],
14181418
'name': 'density',
14191419
'ncontours': 20,
14201420
'reversescale': True,
14211421
'showscale': False,
14221422
'type': 'histogram2dcontour',
14231423
'x': [1, 2],
14241424
'y': [2, 4]},
1425-
{'marker': {'color': 'rgb(255.0, 237.0, 222.0)'},
1425+
{'marker': {'color': 'rgb(255, 237, 222)'},
14261426
'name': 'x density',
14271427
'type': 'histogram',
14281428
'x': [1, 2],
14291429
'yaxis': 'y2'},
1430-
{'marker': {'color': 'rgb(255.0, 237.0, 222.0)'},
1430+
{'marker': {'color': 'rgb(255, 237, 222)'},
14311431
'name': 'y density',
14321432
'type': 'histogram',
14331433
'xaxis': 'x2',

plotly/tests/test_optional/temp-plot.html

Lines changed: 55 additions & 40 deletions
Large diffs are not rendered by default.

plotly/tests/test_optional/test_figure_factory.py

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -843,14 +843,14 @@ def test_trisurf_all_args(self):
843843

844844
exp_trisurf_plot = {
845845
'data': [{'facecolor': np.array(
846-
['rgb(144.0, 94.5, 132.0)',
847-
'rgb(23.0, 190.0, 207.0)',
848-
'rgb(144.0, 94.5, 132.0)',
849-
'rgb(31.0, 119.0, 180.0)',
850-
'rgb(144.0, 94.5, 132.0)',
851-
'rgb(31.0, 119.0, 180.0)',
852-
'rgb(144.0, 94.5, 132.0)',
853-
'rgb(23.0, 190.0, 207.0)']
846+
['rgb(144, 94, 132)',
847+
'rgb(23, 190, 207)',
848+
'rgb(144, 94, 132)',
849+
'rgb(31, 119, 180)',
850+
'rgb(144, 94, 132)',
851+
'rgb(31, 119, 180)',
852+
'rgb(144, 94, 132)',
853+
'rgb(23, 190, 207)']
854854
),
855855
'i': np.array([3, 1, 1, 5, 7, 3, 5, 7]),
856856
'j': np.array([1, 3, 5, 1, 3, 7, 7, 5]),
@@ -884,16 +884,16 @@ def test_trisurf_all_args(self):
884884
-0.0, 0.0, 0.0, None, -0.0, 0.0, -1.0,
885885
-0.0, None, 0.0, 0.0, 0.0, 0.0, None,
886886
0.0, 0.0, 1.0, 0.0, None])},
887-
{'colorscale': [[0.0, 'rgb(31.0, 119.0, 180.0)'],
888-
[0.1111111111111111, 'rgb(255.0, 127.0, 14.0)'],
889-
[0.2222222222222222, 'rgb(44.0, 160.0, 44.0)'],
890-
[0.3333333333333333, 'rgb(214.0, 39.0, 40.0)'],
891-
[0.4444444444444444, 'rgb(148.0, 103.0, 189.0)'],
892-
[0.5555555555555556, 'rgb(140.0, 86.0, 75.0)'],
893-
[0.6666666666666666, 'rgb(227.0, 119.0, 194.0)'],
894-
[0.7777777777777778, 'rgb(127.0, 127.0, 127.0)'],
895-
[0.8888888888888888, 'rgb(188.0, 189.0, 34.0)'],
896-
[1.0, 'rgb(23.0, 190.0, 207.0)']],
887+
{'colorscale': [[0.0, 'rgb(31, 119, 180)'],
888+
[0.1111111111111111, 'rgb(255, 127, 14)'],
889+
[0.2222222222222222, 'rgb(44, 160, 44)'],
890+
[0.3333333333333333, 'rgb(214, 39, 40)'],
891+
[0.4444444444444444, 'rgb(148, 103, 189)'],
892+
[0.5555555555555556, 'rgb(140, 86, 75)'],
893+
[0.6666666666666666, 'rgb(227, 119, 194)'],
894+
[0.7777777777777778, 'rgb(127, 127, 127)'],
895+
[0.8888888888888888, 'rgb(188, 189, 34)'],
896+
[1.0, 'rgb(23, 190, 207)']],
897897
'intensity': [0, 1],
898898
'showscale': True,
899899
'type': 'mesh3d',
@@ -1261,13 +1261,13 @@ def test_scatter_plot_matrix_kwargs(self):
12611261
)
12621262

12631263
exp_scatter_plot_matrix = {
1264-
'data': [{'marker': {'color': 'rgb(128.0, 0.0, 38.0)'},
1264+
'data': [{'marker': {'color': 'rgb(128, 0, 38)'},
12651265
'showlegend': False,
12661266
'type': 'histogram',
12671267
'x': [2, -15, -2, 0],
12681268
'xaxis': 'x1',
12691269
'yaxis': 'y1'},
1270-
{'marker': {'color': 'rgb(255.0, 255.0, 204.0)'},
1270+
{'marker': {'color': 'rgb(255, 255, 204)'},
12711271
'showlegend': False,
12721272
'type': 'histogram',
12731273
'x': [6, 5],
@@ -1324,7 +1324,7 @@ def test_df_dataframe_all_args(self):
13241324
'shapes': [{'opacity': 1,
13251325
'y1': 0.2,
13261326
'xref': 'x',
1327-
'fillcolor': 'rgb(31.0, 119.0, 180.0)',
1327+
'fillcolor': 'rgb(31, 119, 180)',
13281328
'yref': 'y',
13291329
'y0': -0.2,
13301330
'x0': '2009-01-01',
@@ -1334,7 +1334,7 @@ def test_df_dataframe_all_args(self):
13341334
{'opacity': 1,
13351335
'y1': 1.2,
13361336
'xref': 'x',
1337-
'fillcolor': 'rgb(255.0, 127.0, 14.0)',
1337+
'fillcolor': 'rgb(255, 127, 14)',
13381338
'yref': 'y',
13391339
'y0': 0.8,
13401340
'x0': '2009-03-05',
@@ -1543,7 +1543,7 @@ def test_violin_fig(self):
15431543

15441544
exp_violin = {
15451545
'data': [{'fill': 'tonextx',
1546-
'fillcolor': 'rgb(31.0, 119.0, 180.0)',
1546+
'fillcolor': 'rgb(31, 119, 180)',
15471547
'hoverinfo': 'text',
15481548
'line': {'color': 'rgb(0, 0, 0)',
15491549
'shape': 'spline',
@@ -1721,7 +1721,7 @@ def test_violin_fig(self):
17211721
1.96969697, 1.97979798, 1.98989899,
17221722
2.])},
17231723
{'fill': 'tonextx',
1724-
'fillcolor': 'rgb(31.0, 119.0, 180.0)',
1724+
'fillcolor': 'rgb(31, 119, 180)',
17251725
'hoverinfo': 'text',
17261726
'line': {'color': 'rgb(0, 0, 0)',
17271727
'shape': 'spline',
@@ -1921,7 +1921,7 @@ def test_violin_fig(self):
19211921
'x': [0],
19221922
'y': [1.5]},
19231923
{'hoverinfo': 'y',
1924-
'marker': {'color': 'rgb(31.0, 119.0, 180.0)',
1924+
'marker': {'color': 'rgb(31, 119, 180)',
19251925
'symbol': 'line-ew-open'},
19261926
'mode': 'markers',
19271927
'name': '',

plotly/tools.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import six
1616
import math
17+
import decimal
1718

1819
from plotly import utils
1920
from plotly import exceptions
@@ -3177,10 +3178,9 @@ def _find_intermediate_color(lowcolor, highcolor, intermed):
31773178
diff_1 = float(highcolor[1] - lowcolor[1])
31783179
diff_2 = float(highcolor[2] - lowcolor[2])
31793180

3180-
inter_colors = (lowcolor[0] + intermed * diff_0,
3181-
lowcolor[1] + intermed * diff_1,
3182-
lowcolor[2] + intermed * diff_2)
3183-
return inter_colors
3181+
return (lowcolor[0] + intermed * diff_0,
3182+
lowcolor[1] + intermed * diff_1,
3183+
lowcolor[2] + intermed * diff_2)
31843184

31853185
@staticmethod
31863186
def _color_parser(colors, function):
@@ -3220,11 +3220,9 @@ def _unconvert_from_RGB_255(colors):
32203220
a value between 0 and 1
32213221
32223222
"""
3223-
un_rgb_color = (colors[0]/(255.0),
3224-
colors[1]/(255.0),
3225-
colors[2]/(255.0))
3226-
3227-
return un_rgb_color
3223+
return (colors[0]/(255.0),
3224+
colors[1]/(255.0),
3225+
colors[2]/(255.0))
32283226

32293227
@staticmethod
32303228
def _map_face2color(face, colormap, vmin, vmax):
@@ -3263,10 +3261,11 @@ def _map_face2color(face, colormap, vmin, vmax):
32633261
face_color = FigureFactory._find_intermediate_color(
32643262
colormap[low_color_index],
32653263
colormap[low_color_index + 1],
3266-
t * (len(colormap) - 1) - low_color_index)
3264+
t * (len(colormap) - 1) - low_color_index
3265+
)
3266+
32673267
face_color = FigureFactory._convert_to_RGB_255(face_color)
32683268
face_color = FigureFactory._label_rgb(face_color)
3269-
32703269
return face_color
32713270

32723271
@staticmethod
@@ -3335,7 +3334,7 @@ def _trisurf(x, y, z, simplices, show_colorbar, colormap=None,
33353334
facecolor.append(color)
33363335

33373336
# Make sure we have arrays to speed up plotting
3338-
facecolor = np.asarray(facecolor)
3337+
facecolor = list(facecolor)
33393338
ii, jj, kk = simplices.T
33403339

33413340
# make a colorscale from the colors
@@ -4563,9 +4562,24 @@ def _endpts_to_intervals(endpts):
45634562
def _convert_to_RGB_255(colors):
45644563
"""
45654564
Multiplies each element of a triplet by 255
4565+
4566+
Each coordinate of the color tuple is rounded to the nearest float and
4567+
then is turned into an integer. If a number is of the form x.5, then
4568+
if x is odd, the number rounds up to (x+1). Otherwise, it rounds down
4569+
to just x. This is the way rounding works in Python 3 and in current
4570+
statistical analysis to avoid rounding bias
45664571
"""
4572+
rgb_components = []
4573+
4574+
for component in colors:
4575+
rounded_num = decimal.Decimal(str(component*255.0)).quantize(
4576+
decimal.Decimal('1'), rounding=decimal.ROUND_HALF_EVEN
4577+
)
4578+
# convert rounded number to an integer from 'Decimal' form
4579+
rounded_num = int(rounded_num)
4580+
rgb_components.append(rounded_num)
45674581

4568-
return (colors[0]*255.0, colors[1]*255.0, colors[2]*255.0)
4582+
return (rgb_components[0], rgb_components[1], rgb_components[2])
45694583

45704584
@staticmethod
45714585
def _n_colors(lowcolor, highcolor, n_colors):

plotly/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.12.7'
1+
__version__ = '1.12.8'

0 commit comments

Comments
 (0)