8000 Merge pull request #62 from plotly/bubbles · Bobfrat/python-api@9f385c8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f385c8

Browse files
committed
Merge pull request plotly#62 from plotly/bubbles
arrays in matplotlib scatters work now for color, marker, etc.
2 parents e22c3a7 + f5a546d commit 9f385c8

File tree

4 files changed

+71
-38
lines changed

4 files changed

+71
-38
lines changed

plotly/graph_objs/graph_objs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,8 @@ def strip_style(self):
599599
else:
600600
try:
601601
if INFO[obj_key][key]['type'] == 'style':
602-
del self[key]
602+
if not hasattr(self[key], '__iter__'):
603+
del self[key]
603604
except KeyError: # TODO: Update the JSON
604605
# print "'type' not in {} for {}".format(obj_key, key)
605606
pass

plotly/matplotlylib/mpltools.py

Lines changed: 60 additions & 11 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@ def check_corners(inner_obj, outer_obj):
5050
return True
5151

5252

53-
def convert_affine_trans(dpi=None, aff=None):
54-
if aff is not None and dpi is not None:
55-
try:
56-
return aff.to_values()[0]*72/dpi
57-
except AttributeError:
58-
return aff[0][0]*72/dpi
59-
else:
60-
return None
61-
62-
6353
def convert_dash(mpl_dash):
6454
"""Convert mpl line symbol to plotly line symbol and return symbol."""
6555
if mpl_dash in DASH_MAP:
@@ -79,7 +69,12 @@ def convert_path(path):
7969

8070
def convert_symbol(mpl_symbol):
8171
"""Convert mpl marker symbol to plotly symbol and return symbol."""
82-
if mpl_symbol in SYMBOL_MAP:
72+
if isinstance(mpl_symbol, list):
73+
symbol = list()
74+
for s in mpl_symbol:
75+
symbol += [convert_symbol(s)]
76+
return symbol
77+
elif mpl_symbol in SYMBOL_MAP:
8378
return SYMBOL_MAP[mpl_symbol]
8479
else:
8580
return 'dot' # default
@@ -231,6 +226,60 @@ def get_bar_gap(bar_starts, bar_ends, tol=1e-10):
231226
return gap0
232227

233228

229+
def convert_rgba_array(color_list):
230+
clean_color_list = list()
231+
for c in color_list:
232+
clean_color_list += [(dict(r=int(c[0]*255),
233+
g=int(c[1]*255),
234+
b=int(c[2]*255),
235+
a=c[3]
236+
))]
237+
plotly_colors = list()
238+
for rgba in clean_color_list:
239+
plotly_colors += ["rgba({r},{g},{b},{a})".format(**rgba)]
240+
if len(plotly_colors) == 1:
241+
return plotly_colors[0]
242+
else:
243+
return plotly_colors
244+
245+
246+
def convert_path_array(path_array):
247+
symbols = list()
248+
for path in path_array:
249+
symbols += [convert_path(path)]
250+
if len(symbols) == 1:
251+
return symbols[0]
252+
else:
253+
return symbols
254+
255+
def convert_linewidth_array(width_array):
256+
if len(width_array) == 1:
257+
return width_array[0]
258+
else:
259+
return width_array
260+
261+
262+
def convert_size_array(size_array):
263+
size = [math.sqrt(s) for s in size_array]
264+
if len(size) == 1:
265+
return size[0]
266+
else:
267+
return size
268+
269+
270+
def get_markerstyle_from_collection(props):
271+
markerstyle=dict(
272+
alpha=None,
273+
facecolor=convert_rgba_array(props['styles']['facecolor']),
274+
marker=convert_path_array(props['paths']),
275+
edgewidth=convert_linewidth_array(props['styles']['linewidth']),
276+
# markersize=convert_size_array(props['styles']['size']), # TODO!
277+
markersize=convert_size_array(props['mplobj'].get_sizes()),
278+
edgecolor=convert_rgba_array(props['styles']['edgecolor'])
279+
)
280+
return markerstyle
281+
282+
234283
def get_rect_xmin(data):
235284
"""Find minimum x value from four (x,y) vertices."""
236285
return min(data[0][0], data[1][0], data[2][0], data[3][0])

plotly/matplotlylib/renderer.py

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -351,29 +351,12 @@ def draw_path_collection(self, **props):
351351
"""
352352
self.msg += " Attempting to draw a path collection\n"
353353
if props['offset_coordinates'] is 'data':
354-
alpha_face = props['styles']['facecolor'][0][3]
355-
rgb_face = [int(c*255)
356-
for c in props['styles']['facecolor'][0][:3]]
357-
alpha_edge = props['styles']['edgecolor'][0][3]
358-
rgb_edge = [int(c*255)
359-
for c in props['styles']['edgecolor'][0][:3]]
360-
data = props['offsets']
361-
marker = mpltools.convert_path(props['paths'][0])
362-
style = {
363-
'alpha': alpha_face,
364-
'facecolor': 'rgb({},{},{})'.format(*rgb_face),
365-
'marker': marker,
366-
'edgecolor': 'rgb({},{},{})'.format(*rgb_edge),
367-
'edgewidth': props['styles']['linewidth'][0],
368-
'markersize': mpltools.convert_affine_trans(
369-
dpi=self.mpl_fig.get_dpi(),
370-
aff=props['path_transforms'][0])
371-
}
354+
markerstyle = mpltools.get_markerstyle_from_collection(props)
372355
scatter_props = {
373356
'coordinates': 'data',
374-
'data': data,
357+
'data': props['offsets'],
375358
'label': None,
376-
'markerstyle': style,
359+
'markerstyle': markerstyle,
377360
'linestyle': None
378361
}
379362
self.msg += " Drawing path collection as markers\n"

plotly/tests/test_matplotlylib/data/scatter.py

Lines changed: 6 additions & 6 deletions
92
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
marker=Marker(
1717
symbol='dot',
1818
line=Line(
19-
color='rgb(0,0,0)',
19+
color='rgba(0,0,0,1.0)',
2020
width=1.0
2121
),
2222
size=4.4721359549995796,
23-
color='rgb(0,0,255)',
23+
color='rgba(0,0,255,1.0)',
2424
opacity=1.0
2525
),
2626
xaxis='x1',
@@ -84,11 +84,11 @@
8484
marker=Marker(
8585
symbol='triangle-up',
8686
line=Line(
87-
color='rgb(255,0,0)',
87+
color='rgba(255,0,0,0.5)',
8888
width=1.0
8989
),
9090
size=11.0,
91-
color='rgb(255,0,0)',
91+
color='rgba(255,0,0,0.5)',
92
opacity=0.5
9393
),
9494
xaxis='x1',
@@ -101,11 +101,11 @@
101101
marker=Marker(
102102
symbol='square',
103103
line=Line(
104-
color='rgb(128,0,128)',
104+
color='rgba(128,0,128,0.5)',
105105
width=1.0
106106
),
107107
size=8.0,
108-
color='rgb(128,0,128)',
108+
color='rgba(128,0,128,0.5)',
109109
opacity=0.5
110110
),
111111
xaxis='x1',

0 commit comments

Comments
 (0)
0