Skip to content

Commit ccc92da

Browse files
committed
added histogram to facet_grid
1 parent 3ddd699 commit ccc92da

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

plotly/figure_factory/_facet_grid.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
THRES_FOR_FLIPPED_FACET_TITLES = 10
2626
GRID_WIDTH = 1
2727

28+
VALID_TRACE_TYPES = ['scatter', 'scattergl', 'histogram']
2829

2930
CUSTOM_LABEL_ERROR = (
3031
"If you are using a dictionary for custom labels for the facet row/col, "
@@ -176,7 +177,7 @@ def _add_shapes_to_fig(fig, annot_rect_color, flipped_rows=False,
176177
def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
177178
colormap, num_of_rows,
178179
num_of_cols, facet_row_labels,
179-
facet_col_labels, trace_type,
180+
facet_col_labels, temp_trace,
180181
flipped_rows, flipped_cols, show_boxes,
181182
marker_color, kwargs_trace, kwargs_marker):
182183

@@ -193,7 +194,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
193194
x=group[1][x],
194195
y=group[1][y],
195196
mode='markers',
196-
type=trace_type,
197+
type=temp_trace,
197198
name=group[0],
198199
marker=dict(
199200
color=colormap[group[0]],
@@ -214,7 +215,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
214215
x=data_by_color[x],
215216
y=data_by_color[y],
216217
mode='markers',
217-
type=trace_type,
218+
type=temp_trace,
218219
name=color_val,
219220
marker=dict(
220221
color=colormap[color_val],
@@ -265,7 +266,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
265266
x=group_filtered[x],
266267
y=group_filtered[y],
267268
mode='markers',
268-
type=trace_type,
269+
type=temp_trace,
269270
name=color_val,
270271
marker=dict(
271272
color=colormap[color_val],
@@ -278,7 +279,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
278279
x=group[x],
279280
y=group[y],
280281
mode='markers',
281-
type=trace_type,
282+
type=temp_trace,
282283
name=color_val,
283284
marker=dict(
284285
color=colormap[color_val],
@@ -311,7 +312,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name,
311312
def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
312313
colormap, num_of_rows,
313314
num_of_cols, facet_row_labels,
314-
facet_col_labels, trace_type,
315+
facet_col_labels, temp_trace,
315316
flipped_rows, flipped_cols, show_boxes,
316317
marker_color, kwargs_trace, kwargs_marker):
317318

@@ -326,7 +327,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
326327
x=df[x],
327328
y=df[y],
328329
mode='markers',
329-
type=trace_type,
330+
type=temp_trace,
330331
marker=dict(
331332
color=df[color_name],
332333
colorscale=colormap,
@@ -346,7 +347,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
346347
x=group[1][x],
347348
y=group[1][y],
348349
mode='markers',
349-
type=trace_type,
350+
type=temp_trace,
350351
marker=dict(
351352
color=df[color_name],
352353
colorscale=colormap,
@@ -396,7 +397,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
396397
x=group[x],
397398
y=group[y],
398399
mode='markers',
399-
type=trace_type,
400+
type=temp_trace,
400401
marker=dict(
401402
color=df[color_name],
402403
colorscale=colormap,
@@ -411,7 +412,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
411412
x=group[x],
412413
y=group[y],
413414
mode='markers',
414-
type=trace_type,
415+
type=temp_trace,
415416
showlegend=False,
416417
**kwargs
417418
)
@@ -439,7 +440,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name,
439440

440441
def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows,
441442
num_of_cols, facet_row_labels, facet_col_labels,
442-
trace_type, flipped_rows, flipped_cols, show_boxes,
443+
temp_trace, flipped_rows, flipped_cols, show_boxes,
443444
marker_color, kwargs_trace, kwargs_marker):
444445

445446
fig = make_subplots(rows=num_of_rows, cols=num_of_cols,
@@ -452,7 +453,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows,
452453
x=df[x],
453454
y=df[y],
454455
mode='markers',
455-
type=trace_type,
456+
type=temp_trace,
456457
marker=dict(
457458
color=marker_color,
458459
**kwargs_marker
@@ -470,7 +471,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows,
470471
x=group[1][x],
471472
y=group[1][y],
472473
mode='markers',
473-
type=trace_type,
474+
type=temp_trace,
474475
marker=dict(
475476
color=marker_color,
476477
**kwargs_marker
@@ -514,7 +515,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows,
514515
x=group[x],
515516
y=group[y],
516517
mode='markers',
517-
type=trace_type,
518+
type=temp_trace,
518519
marker=dict(
519520
color=marker_color,
520521
**kwargs_marker
@@ -581,7 +582,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
581582
:param (int) height: the height of the facet grid figure.
582583
:param (int) width: the width of the facet grid figure.
583584
:param (str) trace_type: decides the type of plot to appear in the
584-
facet grid. The options are 'scatter' and 'scattergl'.
585+
facet grid. The options are 'scatter', 'scattergl' and 'histogram'.
585586
Default = 'scatter'.
586587
:param (str) scales: determines if axes have fixed ranges or not. Valid
587588
settings are 'fixed' (all axes fixed), 'free_x' (x axis free only),
@@ -726,11 +727,13 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
726727
"'scales' must be set to 'fixed', 'free_x', 'free_y' and 'free'."
727728
)
728729

729-
if trace_type not in ['scatter', 'scattergl']:
730+
if trace_type not in VALID_TRACE_TYPES:
730731
raise exceptions.PlotlyError(
731-
"'trace_type' must be 'scatter' or 'scattergl'."
732+
"'trace_type' must be in {}".format(VALID_TRACE_TYPES)
732733
)
733734

735+
temp_trace = 'scatter' if (trace_type == 'histogram') else trace_type
736+
734737
# seperate kwargs for marker and else
735738
if 'marker' in kwargs:
736739
kwargs_marker = kwargs['marker']
@@ -808,7 +811,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
808811
fig = _facet_grid_color_categorical(
809812
df, x, y, facet_row, facet_col, color_name, colormap,
810813
num_of_rows, num_of_cols, facet_row_labels, facet_col_labels,
811-
trace_type, flipped_rows, flipped_cols, show_boxes,
814+
temp_trace, flipped_rows, flipped_cols, show_boxes,
812815
marker_color, kwargs_trace, kwargs_marker
813816
)
814817

@@ -827,7 +830,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
827830
fig = _facet_grid_color_categorical(
828831
df, x, y, facet_row, facet_col, color_name, colormap,
829832
num_of_rows, num_of_cols, facet_row_labels,
830-
facet_col_labels, trace_type, flipped_rows,
833+
facet_col_labels, temp_trace, flipped_rows,
831834
flipped_cols, show_boxes, marker_color, kwargs_trace,
832835
kwargs_marker
833836
)
@@ -839,7 +842,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
839842
fig = _facet_grid_color_numerical(
840843
df, x, y, facet_row, facet_col, color_name,
841844
colorscale_list, num_of_rows, num_of_cols,
842-
facet_row_labels, facet_col_labels, trace_type,
845+
facet_row_labels, facet_col_labels, temp_trace,
843846
flipped_rows, flipped_cols, show_boxes, marker_color,
844847
kwargs_trace, kwargs_marker
845848
)
@@ -855,7 +858,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
855858
fig = _facet_grid_color_numerical(
856859
df, x, y, facet_row, facet_col, color_name,
857860
colorscale_list, num_of_rows, num_of_cols,
858-
facet_row_labels, facet_col_labels, trace_type,
861+
facet_row_labels, facet_col_labels, temp_trace,
859862
flipped_rows, flipped_cols, show_boxes, marker_color,
860863
kwargs_trace, kwargs_marker
861864
)
@@ -864,15 +867,15 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
864867
fig = _facet_grid_color_numerical(
865868
df, x, y, facet_row, facet_col, color_name,
866869
colorscale_list, num_of_rows, num_of_cols,
867-
facet_row_labels, facet_col_labels, trace_type,
870+
facet_row_labels, facet_col_labels, temp_trace,
868871
flipped_rows, flipped_cols, show_boxes, marker_color,
869872
kwargs_trace, kwargs_marker
870873
)
871874

872875
else:
873876
fig = _facet_grid(
874877
df, x, y, facet_row, facet_col, num_of_rows, num_of_cols,
875-
facet_row_labels, facet_col_labels, trace_type, flipped_rows,
878+
facet_row_labels, facet_col_labels, temp_trace, flipped_rows,
876879
flipped_cols, show_boxes, marker_color, kwargs_trace,
877880
kwargs_marker
878881
)
@@ -1012,4 +1015,10 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None,
10121015
if '{}axis'.format(x_y) in key and range_are_numbers:
10131016
fig['layout'][key]['range'] = [min_range, max_range]
10141017

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+
10151024
return fig

plotly/tests/test_optional/test_figure_factory.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,11 +1950,8 @@ def test_valid_col_selection(self):
19501950
def test_valid_trace_type(self):
19511951
data = pd.DataFrame([[0, 0], [1, 1]], columns=['a', 'b'])
19521952

1953-
pattern = "'trace_type' must be 'scatter' or 'scattergl'."
1954-
1955-
self.assertRaisesRegexp(PlotlyError, pattern,
1956-
ff.create_facet_grid,
1957-
data, 'a', 'b', trace_type='bar')
1953+
self.assertRaises(PlotlyError, ff.create_facet_grid,
1954+
data, 'a', 'b', trace_type='foo')
19581955

19591956
def test_valid_scales(self):
19601957
data = pd.DataFrame([[0, 0], [1, 1]], columns=['a', 'b'])

0 commit comments

Comments
 (0)