Skip to content

Commit 3be914a

Browse files
committed
chelse's comments
1 parent bda26e1 commit 3be914a

File tree

2 files changed

+93
-61
lines changed

2 files changed

+93
-61
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## [Unreleased]
66
### Added
7-
- `.create_facet_grid` now supports histogram traces.
7+
- `figure_factory.create_facet_grid` now supports histogram traces.
88

99
## [2.0.10] - 2017-06-12
1010
### Added

plotly/figure_factory/_facet_grid.py

Lines changed: 92 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def _add_shapes_to_fig(fig, annot_rect_color, flipped_rows=False,
177177
def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
178178
colormap, num_of_rows,
179179
num_of_cols, facet_row_labels,
180-
facet_col_labels, temp_trace,
180+
facet_col_labels, trace_type,
181181
flipped_rows, flipped_cols, show_boxes,
182182
marker_color, kwargs_trace, kwargs_marker):
183183

@@ -190,18 +190,22 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
190190
if not facet_row and not facet_col:
191191
color_groups = list(df.groupby(color_name))
192192
for group in color_groups:
193-
trace = graph_objs.Scatter(
193+
trace = dict(
194194
x=group[1][x],
195195
y=group[1][y],
196-
mode='markers',
197-
type=temp_trace,
196+
type=trace_type,
198197
name=group[0],
199198
marker=dict(
200199
color=colormap[group[0]],
201-
**kwargs_marker
202200
),
203201
**kwargs_trace
204202
)
203+
if trace_type in ['scatter', 'scattergl']:
204+
trace['mode'] = 'markers'
205+
trace['marker'] = dict(
206+
color=colormap[group[0]], **kwargs_marker
207+
)
208+
205209
fig.append_trace(trace, 1, 1)
206210

207211
elif (facet_row and not facet_col) or (not facet_row and facet_col):
@@ -211,18 +215,22 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
211215
for j, group in enumerate(groups_by_facet):
212216
for color_val in df[color_name].unique():
213217
data_by_color = group[1][group[1][color_name] == color_val]
214-
trace = graph_objs.Scatter(
218+
trace = dict(
215219
x=data_by_color[x],
216220
y=data_by_color[y],
217-
mode='markers',
218-
type=temp_trace,
221+
type=trace_type,
219222
name=color_val,
220223
marker=dict(
221224
color=colormap[color_val],
222-
**kwargs_marker
223225
),
224226
**kwargs_trace
225227
)
228+
if trace_type in ['scatter', 'scattergl']:
229+
trace['mode'] = 'markers'
230+
trace['marker'] = dict(
231+
color=colormap[group[0]], **kwargs_marker
232+
)
233+
226234
fig.append_trace(trace,
227235
j + 1 if facet_row else 1,
228236
1 if facet_row else j + 1)
@@ -262,32 +270,41 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
262270
if group.values.tolist() != [[None, None, None]]:
263271
group_filtered = group[group[color_name] == color_val]
264272

265-
trace = graph_objs.Scatter(
273+
trace = dict(
266274
x=group_filtered[x],
267275
y=group_filtered[y],
268-
mode='markers',
269-
type=temp_trace,
276+
type=trace_type,
270277
name=color_val,
271278
marker=dict(
272279
color=colormap[color_val],
273-
**kwargs_marker
274280
),
275281
**kwargs_trace
276282
)
283+
if trace_type in ['scatter', 'scattergl']:
284+
trace['mode'] = 'markers'
285+
trace['marker'] = dict(
286+
color=colormap[color_val], **kwargs_marker
287+
)
288+
277289
else:
278-
trace = graph_objs.Scatter(
290+
trace = dict(
279291
x=group[x],
280292
y=group[y],
281-
mode='markers',
282-
type=temp_trace,
293+
type=trace_type,
283294
name=color_val,
284295
marker=dict(
285296
color=colormap[color_val],
286-
**kwargs_marker
287297
),
288298
showlegend=False,
289299
**kwargs_trace
290300
)
301+
if trace_type in ['scatter', 'scattergl']:
302+
trace['mode'] = 'markers'
303+
trace['marker'] = dict(
304+
color=colormap[color_val], **kwargs_marker
305+
)
306+
307+
291308
fig.append_trace(trace, row_count + 1, col_count + 1)
292309
if row_count == 0:
293310
label = _return_label(col_values[col_count],
@@ -312,7 +329,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
312329
def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
313330
colormap, num_of_rows,
314331
num_of_cols, facet_row_labels,
315-
facet_col_labels, temp_trace,
332+
facet_col_labels, trace_type,
316333
flipped_rows, flipped_cols, show_boxes,
317334
marker_color, kwargs_trace, kwargs_marker):
318335

@@ -323,40 +340,48 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
323340

324341
annotations = []
325342
if not facet_row and not facet_col:
326-
trace = graph_objs.Scatter(
343+
trace = dict(
327344
x=df[x],
328345
y=df[y],
329-
mode='markers',
330-
type=temp_trace,
346+
type=trace_type,
331347
marker=dict(
332348
color=df[color_name],
333349
colorscale=colormap,
334350
showscale=True,
335-
**kwargs_marker
336351
),
337352
**kwargs_trace
338353
)
354+
if trace_type in ['scatter', 'scattergl']:
355+
trace['mode'] = 'markers'
356+
trace['marker'] = dict(
357+
color=df[color_name], **kwargs_marker
358+
)
359+
339360
fig.append_trace(trace, 1, 1)
340361

341362
if (facet_row and not facet_col) or (not facet_row and facet_col):
342363
groups_by_facet = list(
343364
df.groupby(facet_row if facet_row else facet_col)
344365
)
345366
for j, group in enumerate(groups_by_facet):
346-
trace = graph_objs.Scatter(
367+
trace = dict(
347368
x=group[1][x],
348369
y=group[1][y],
349-
mode='markers',
350-
type=temp_trace,
370+
type=trace_type,
351371
marker=dict(
352372
color=df[color_name],
353373
colorscale=colormap,
354374
showscale=True,
355375
colorbar=dict(x=1.15),
356-
**kwargs_marker
357376
),
358377
**kwargs_trace
359378
)
379+
if trace_type in ['scatter', 'scattergl']:
380+
trace['mode'] = 'markers'
381+
trace['marker'] = dict(
382+
color=df[color_name], **kwargs_marker
383+
)
384+
360385
fig.append_trace(
361386
trace,
362387
j + 1 if facet_row else 1,
@@ -393,29 +418,33 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
393418
columns=[x, y, color_name])
394419

395420
if group.values.tolist() != [[None, None, None]]:
396-
trace = graph_objs.Scatter(
421+
trace = dict(
397422
x=group[x],
398423
y=group[y],
399-
mode='markers',
400-
type=temp_trace,
424+
type=trace_type,
401425
marker=dict(
402426
color=df[color_name],
403427
colorscale=colormap,
404428
showscale=(row_count == 0),
405429
colorbar=dict(x=1.15),
406-
**kwargs_marker
407430
),
408431
**kwargs_trace
409432
)
433+
if trace_type in ['scatter', 'scattergl']:
434+
trace['mode'] = 'markers'
435+
trace['marker'] = dict(
436+
color=df[color_name], **kwargs_marker
437+
)
438+
410439
else:
411-
trace = graph_objs.Scatter(
440+
trace = dict(
412441
x=group[x],
413442
y=group[y],
414-
mode='markers',
415-
type=temp_trace,
443+
type=trace_type,
416444
showlegend=False,
417445
**kwargs
418446
)
447+
419448
fig.append_trace(trace, row_count + 1, col_count + 1)
420449
if row_count == 0:
421450
label = _return_label(col_values[col_count],
@@ -440,7 +469,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
440469

441470
def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows,
442471
num_of_cols, facet_row_labels, facet_col_labels,
443-
temp_trace, flipped_rows, flipped_cols, show_boxes,
472+
trace_type, flipped_rows, flipped_cols, show_boxes,
444473
marker_color, kwargs_trace, kwargs_marker):
445474

446475
fig = make_subplots(rows=num_of_rows, cols=num_of_cols,
@@ -449,35 +478,39 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows,
449478
vertical_spacing=SUBPLOT_SPACING, print_grid=False)
450479
annotations = []
451480
if not facet_row and not facet_col:
452-
trace = graph_objs.Scatter(
481+
trace = dict(
453482
x=df[x],
454483
y=df[y],
455-
mode='markers',
456-
type=temp_trace,
484+
type=trace_type,
457485
marker=dict(
458486
color=marker_color,
459-
**kwargs_marker
460487
),
461488
**kwargs_trace
462489
)
490+
if trace_type in ['scatter', 'scattergl']:
491+
trace['mode'] = 'markers'
492+
trace['marker'] = dict(color=marker_color, **kwargs_marker)
493+
463494
fig.append_trace(trace, 1, 1)
464495

465496
elif (facet_row and not facet_col) or (not facet_row and facet_col):
466497
groups_by_facet = list(
467498
df.groupby(facet_row if facet_row else facet_col)
468499
)
469500
for j, group in enumerate(groups_by_facet):
470-
trace = graph_objs.Scatter(
501+
trace = dict(
471502
x=group[1][x],
472503
y=group[1][y],
473-
mode='markers',
474-
type=temp_trace,
504+
type=trace_type,
475505
marker=dict(
476506
color=marker_color,
477-
**kwargs_marker
478507
),
479508
**kwargs_trace
480509
)
510+
if trace_type in ['scatter', 'scattergl']:
511+
trace['mode'] = 'markers'
512+
trace['marker'] = dict(color=marker_color, **kwargs_marker)
513+
481514
fig.append_trace(trace,
482515
j + 1 if facet_row else 1,
483516
1 if facet_row else j + 1)
@@ -511,17 +544,21 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows,
511544
group = tuple_to_facet_group[(x_val, y_val)]
512545
except KeyError:
513546
group = pd.DataFrame([[None, None]], columns=[x, y])
514-
trace = graph_objs.Scatter(
547+
trace = dict(
515548
x=group[x],
516549
y=group[y],
517-
mode='markers',
518-
type=temp_trace,
550+
type=trace_type,
519551
marker=dict(
520552
color=marker_color,
521-
**kwargs_marker
522553
),
523554
**kwargs_trace
524555
)
556+
if trace_type in ['scatter', 'scattergl']:
557+
trace['mode'] = 'markers'
558+
trace['marker'] = dict(
559+
color=marker_color, **kwargs_marker
560+
)
561+
525562
fig.append_trace(trace, row_count + 1, col_count + 1)
526563
if row_count == 0:
527564
label = _return_label(col_values[col_count],
@@ -720,6 +757,9 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
720757
"x, y, facet_row, facet_col and color_name must be keys "
721758
"in your dataframe."
722759
)
760+
# autoscale histogram bars
761+
if trace_type == 'histogram':
762+
scales = 'free'
723763

724764
# validate scales
725765
if scales not in ['fixed', 'free_x', 'free_y', 'free']:
@@ -732,8 +772,6 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
732772
"'trace_type' must be in {}".format(VALID_TRACE_TYPES)
733773
)
734774

735-
temp_trace = 'scatter' if (trace_type == 'histogram') else trace_type
736-
737775
# seperate kwargs for marker and else
738776
if 'marker' in kwargs:
739777
kwargs_marker = kwargs['marker']
@@ -811,7 +849,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
811849
fig = _facet_grid_color_categorical(
812850
df, x, y, facet_row, facet_col, color_name, colormap,
813851
num_of_rows, num_of_cols, facet_row_labels, facet_col_labels,
814-
temp_trace, flipped_rows, flipped_cols, show_boxes,
852+
trace_type, flipped_rows, flipped_cols, show_boxes,
815853
marker_color, kwargs_trace, kwargs_marker
816854
)
817855

@@ -830,7 +868,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
830868
fig = _facet_grid_color_categorical(
831869
df, x, y, facet_row, facet_col, color_name, colormap,
832870
num_of_rows, num_of_cols, facet_row_labels,
833-
facet_col_labels, temp_trace, flipped_rows,
871+
facet_col_labels, trace_type, flipped_rows,
834872
flipped_cols, show_boxes, marker_color, kwargs_trace,
835873
kwargs_marker
836874
)
@@ -842,7 +880,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
842880
fig = _facet_grid_color_numerical(
843881
df, x, y, facet_row, facet_col, color_name,
844882
colorscale_list, num_of_rows, num_of_cols,
845-
facet_row_labels, facet_col_labels, temp_trace,
883+
facet_row_labels, facet_col_labels, trace_type,
846884
flipped_rows, flipped_cols, show_boxes, marker_color,
847885
kwargs_trace, kwargs_marker
848886
)
@@ -858,7 +896,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
858896
fig = _facet_grid_color_numerical(
859897
df, x, y, facet_row, facet_col, color_name,
860898
colorscale_list, num_of_rows, num_of_cols,
861-
facet_row_labels, facet_col_labels, temp_trace,
899+
facet_row_labels, facet_col_labels, trace_type,
862900
flipped_rows, flipped_cols, show_boxes, marker_color,
863901
kwargs_trace, kwargs_marker
864902
)
@@ -867,15 +905,15 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
867905
fig = _facet_grid_color_numerical(
868906
df, x, y, facet_row, facet_col, color_name,
869907
colorscale_list, num_of_rows, num_of_cols,
870-
facet_row_labels, facet_col_labels, temp_trace,
908+
facet_row_labels, facet_col_labels, trace_type,
871909
flipped_rows, flipped_cols, show_boxes, marker_color,
872910
kwargs_trace, kwargs_marker
873911
)
874912

875913
else:
876914
fig = _facet_grid(
877915
df, x, y, facet_row, facet_col, num_of_rows, num_of_cols,
878-
facet_row_labels, facet_col_labels, temp_trace, flipped_rows,
916+
facet_row_labels, facet_col_labels, trace_type, flipped_rows,
879917
flipped_cols, show_boxes, marker_color, kwargs_trace,
880918
kwargs_marker
881919
)
@@ -1015,10 +1053,4 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
10151053
if '{}axis'.format(x_y) in key and range_are_numbers:
10161054
fig['layout'][key]['range'] = [min_range, max_range]
10171055

1018-
if trace_type == 'histogram':
1019-
for trace in fig['data']:
1020-
trace['type'] = trace_type
1021-
del trace['marker']['size']
1022-
del trace['mode']
1023-
10241056
return fig

0 commit comments

Comments
 (0)