Skip to content

Commit 3b2d398

Browse files
committed
(matplotlylib) Merge opacity into color.
`Line` `opacity` has been removed (or never existed?). We keep the functionality by merging the hex color and opacity into an rgba tuple.
1 parent 793bfa9 commit 3b2d398

6 files changed

Lines changed: 54 additions & 22 deletions

File tree

plotly/matplotlylib/mpltools.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,43 @@ def convert_symbol(mpl_symbol):
8383
return 'dot' # default
8484

8585

86+
def hex_to_rgb(value):
87+
"""
88+
Change a hex color to an rgb tuple
89+
90+
:param (str|unicode) value: The hex string we want to convert.
91+
:return: (int, int, int) The red, green, blue int-tuple.
92+
93+
Example:
94+
95+
'#FFFFFF' --> (255, 255, 255)
96+
97+
"""
98+
value = value.lstrip('#')
99+
lv = len(value)
100+
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
101+
102+
103+
def merge_color_and_opacity(color, opacity):
104+
"""
105+
Merge hex color with an alpha (opacity) to get an rgba tuple.
106+
107+
:param (str|unicode) color: A hex color string.
108+
:param (float|int) opacity: A value [0, 1] for the 'a' in 'rgba'.
109+
:return: (int, int, int, float) The rgba color and alpha tuple.
110+
111+
"""
112+
if color is None: # None can be used as a placeholder, just bail.
113+
return None
114+
115+
rgb_tup = hex_to_rgb(color)
116+
if opacity is None:
117+
return 'rgb {}'.format(rgb_tup)
118+
119+
rgba_tup = rgb_tup + (opacity,)
120+
return 'rgba {}'.format(rgba_tup)
121+
122+
86123
def convert_va(mpl_va):
87124
"""Convert mpl vertical alignment word to equivalent HTML word.
88125

plotly/matplotlylib/renderer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,11 @@ def draw_marked_line(self, **props):
341341
self.msg += "... with just markers\n"
342342
mode = "markers"
343343
if props['linestyle']:
344+
color = \
345+
mpltools.merge_color_and_opacity(props['linestyle']['color'],
346+
props['linestyle']['alpha'])
344347
line = go.Line(
345-
opacity=props['linestyle']['alpha'],
346-
color=props['linestyle']['color'],
348+
color=color,
347349
width=props['linestyle']['linewidth'],
348350
dash=mpltools.convert_dash(props['linestyle']['dasharray'])
349351
)

plotly/tests/test_optional/test_matplotlylib/data/annotations.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
mode='lines',
1313
line=Line(
1414
dash='solid',
15-
color='#0000FF',
16-
width=1.0,
17-
opacity=1
15+
color='rgba (0, 0, 255, 1)',
16+
width=1.0
1817
),
1918
xaxis='x1',
2019
yaxis='y1'
@@ -26,9 +25,8 @@
2625
mode='lines',
2726
line=Line(
2827
dash='solid',
29-
color='#0000FF',
30-
width=1.0,
31-
opacity=1
28+
color='rgba (0, 0, 255, 1)',
29+
width=1.0
3230
),
3331
xaxis='x1',
3432
yaxis='y1'

plotly/tests/test_optional/test_matplotlylib/data/axis_scales.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
mode='lines',
1717
line=Line(
1818
dash='solid',
19-
color='#0000FF',
20-
width=1.0,
21-
opacity=1
19+
color='rgba (0, 0, 255, 1)',
20+
width=1.0
2221
),
2322
xaxis='x1',
2423
yaxis='y1'

plotly/tests/test_optional/test_matplotlylib/data/lines.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
mode='lines',
2020
line=Line(
2121
dash='solid',
22-
color='#0000FF',
23-
width=1.0,
24-
opacity=1
22+
color='rgba (0, 0, 255, 1)',
23+
width=1.0
2524
),
2625
xaxis='x1',
2726
yaxis='y1'
@@ -102,9 +101,8 @@
102101
mode='lines',
103102
line=Line(
104103
dash='solid',
105-
color='#0000FF',
106-
width=2,
107-
opacity=0.7
104+
color='rgba (0, 0, 255, 0.7)',
105+
width=2
108106
),
109107
xaxis='x1',
110108
yaxis='y1'
@@ -134,9 +132,8 @@
134132
mode='lines',
135133
line=Line(
136134
dash='dash',
137-
color='#FF0000',
138-
width=2,
139-
opacity=0.8
135+
color='rgba (255, 0, 0, 0.8)',
136+
width=2
140137
),
141138
xaxis='x1',
142139
yaxis='y1'

plotly/tests/test_optional/test_matplotlylib/data/subplots.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
line=Line(
1919
dash='solid',
2020
color='#0000FF',
21-
width=1.0,
22-
opacity=1
21+
width=1.0
2322
),
2423
xaxis='x1',
2524
yaxis='y1'

0 commit comments

Comments
 (0)