From ccc92daeae939bf1de514c516b54f285502eb787 Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Tue, 20 Jun 2017 12:39:18 -0400 Subject: [PATCH 01/14] added histogram to facet_grid --- plotly/figure_factory/_facet_grid.py | 55 +++++++++++-------- .../test_optional/test_figure_factory.py | 7 +-- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index cc517cfb611..adef79273fb 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -25,6 +25,7 @@ THRES_FOR_FLIPPED_FACET_TITLES = 10 GRID_WIDTH = 1 +VALID_TRACE_TYPES = ['scatter', 'scattergl', 'histogram'] CUSTOM_LABEL_ERROR = ( "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, def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, - facet_col_labels, trace_type, + facet_col_labels, temp_trace, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker): @@ -193,7 +194,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, x=group[1][x], y=group[1][y], mode='markers', - type=trace_type, + type=temp_trace, name=group[0], marker=dict( color=colormap[group[0]], @@ -214,7 +215,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, x=data_by_color[x], y=data_by_color[y], mode='markers', - type=trace_type, + type=temp_trace, name=color_val, marker=dict( color=colormap[color_val], @@ -265,7 +266,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, x=group_filtered[x], y=group_filtered[y], mode='markers', - type=trace_type, + type=temp_trace, name=color_val, marker=dict( color=colormap[color_val], @@ -278,7 +279,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, x=group[x], y=group[y], mode='markers', - type=trace_type, + type=temp_trace, name=color_val, marker=dict( color=colormap[color_val], @@ -311,7 +312,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, - facet_col_labels, trace_type, + facet_col_labels, temp_trace, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker): @@ -326,7 +327,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, x=df[x], y=df[y], mode='markers', - type=trace_type, + type=temp_trace, marker=dict( color=df[color_name], colorscale=colormap, @@ -346,7 +347,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, x=group[1][x], y=group[1][y], mode='markers', - type=trace_type, + type=temp_trace, marker=dict( color=df[color_name], colorscale=colormap, @@ -396,7 +397,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, x=group[x], y=group[y], mode='markers', - type=trace_type, + type=temp_trace, marker=dict( color=df[color_name], colorscale=colormap, @@ -411,7 +412,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, x=group[x], y=group[y], mode='markers', - type=trace_type, + type=temp_trace, showlegend=False, **kwargs ) @@ -439,7 +440,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, - trace_type, flipped_rows, flipped_cols, show_boxes, + temp_trace, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker): 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, x=df[x], y=df[y], mode='markers', - type=trace_type, + type=temp_trace, marker=dict( color=marker_color, **kwargs_marker @@ -470,7 +471,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, x=group[1][x], y=group[1][y], mode='markers', - type=trace_type, + type=temp_trace, marker=dict( color=marker_color, **kwargs_marker @@ -514,7 +515,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, x=group[x], y=group[y], mode='markers', - type=trace_type, + type=temp_trace, marker=dict( color=marker_color, **kwargs_marker @@ -581,7 +582,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, :param (int) height: the height of the facet grid figure. :param (int) width: the width of the facet grid figure. :param (str) trace_type: decides the type of plot to appear in the - facet grid. The options are 'scatter' and 'scattergl'. + facet grid. The options are 'scatter', 'scattergl' and 'histogram'. Default = 'scatter'. :param (str) scales: determines if axes have fixed ranges or not. Valid 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, "'scales' must be set to 'fixed', 'free_x', 'free_y' and 'free'." ) - if trace_type not in ['scatter', 'scattergl']: + if trace_type not in VALID_TRACE_TYPES: raise exceptions.PlotlyError( - "'trace_type' must be 'scatter' or 'scattergl'." + "'trace_type' must be in {}".format(VALID_TRACE_TYPES) ) + temp_trace = 'scatter' if (trace_type == 'histogram') else trace_type + # seperate kwargs for marker and else if 'marker' in kwargs: kwargs_marker = kwargs['marker'] @@ -808,7 +811,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_categorical( df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, - trace_type, flipped_rows, flipped_cols, show_boxes, + temp_trace, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -827,7 +830,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_categorical( df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, - facet_col_labels, trace_type, flipped_rows, + facet_col_labels, temp_trace, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -839,7 +842,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_numerical( df, x, y, facet_row, facet_col, color_name, colorscale_list, num_of_rows, num_of_cols, - facet_row_labels, facet_col_labels, trace_type, + facet_row_labels, facet_col_labels, temp_trace, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -855,7 +858,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_numerical( df, x, y, facet_row, facet_col, color_name, colorscale_list, num_of_rows, num_of_cols, - facet_row_labels, facet_col_labels, trace_type, + facet_row_labels, facet_col_labels, temp_trace, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -864,7 +867,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_numerical( df, x, y, facet_row, facet_col, color_name, colorscale_list, num_of_rows, num_of_cols, - facet_row_labels, facet_col_labels, trace_type, + facet_row_labels, facet_col_labels, temp_trace, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -872,7 +875,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, else: fig = _facet_grid( df, x, y, facet_row, facet_col, num_of_rows, num_of_cols, - facet_row_labels, facet_col_labels, trace_type, flipped_rows, + facet_row_labels, facet_col_labels, temp_trace, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -1012,4 +1015,10 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, if '{}axis'.format(x_y) in key and range_are_numbers: fig['layout'][key]['range'] = [min_range, max_range] + if trace_type == 'histogram': + for trace in fig['data']: + trace['type'] = trace_type + del trace['marker']['size'] + del trace['mode'] + return fig diff --git a/plotly/tests/test_optional/test_figure_factory.py b/plotly/tests/test_optional/test_figure_factory.py index db83c54519a..34fc1d4117a 100644 --- a/plotly/tests/test_optional/test_figure_factory.py +++ b/plotly/tests/test_optional/test_figure_factory.py @@ -1950,11 +1950,8 @@ def test_valid_col_selection(self): def test_valid_trace_type(self): data = pd.DataFrame([[0, 0], [1, 1]], columns=['a', 'b']) - pattern = "'trace_type' must be 'scatter' or 'scattergl'." - - self.assertRaisesRegexp(PlotlyError, pattern, - ff.create_facet_grid, - data, 'a', 'b', trace_type='bar') + self.assertRaises(PlotlyError, ff.create_facet_grid, + data, 'a', 'b', trace_type='foo') def test_valid_scales(self): data = pd.DataFrame([[0, 0], [1, 1]], columns=['a', 'b']) From bda26e164e59a90c9672ff8c3bf5ddce1d7bf92e Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Tue, 20 Jun 2017 13:19:42 -0400 Subject: [PATCH 02/14] changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8e39dfb35c..282caf324ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] +### Added +- `.create_facet_grid` now supports histogram traces. + ## [2.0.10] - 2017-06-12 ### Added - The figure_factory can now create facet grids with `.create_facet_grid`. Check it out with: From 3be914a461fd4a38509bdd6514f9773890a6685a Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Tue, 20 Jun 2017 16:24:54 -0400 Subject: [PATCH 03/14] chelse's comments --- CHANGELOG.md | 2 +- plotly/figure_factory/_facet_grid.py | 152 ++++++++++++++++----------- 2 files changed, 93 insertions(+), 61 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 282caf324ef..cb8ea52b864 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Added -- `.create_facet_grid` now supports histogram traces. +- `figure_factory.create_facet_grid` now supports histogram traces. ## [2.0.10] - 2017-06-12 ### Added diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index adef79273fb..86bb6555ed2 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -177,7 +177,7 @@ def _add_shapes_to_fig(fig, annot_rect_color, flipped_rows=False, def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, - facet_col_labels, temp_trace, + facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker): @@ -190,18 +190,22 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, if not facet_row and not facet_col: color_groups = list(df.groupby(color_name)) for group in color_groups: - trace = graph_objs.Scatter( + trace = dict( x=group[1][x], y=group[1][y], - mode='markers', - type=temp_trace, + type=trace_type, name=group[0], marker=dict( color=colormap[group[0]], - **kwargs_marker ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict( + color=colormap[group[0]], **kwargs_marker + ) + fig.append_trace(trace, 1, 1) 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, for j, group in enumerate(groups_by_facet): for color_val in df[color_name].unique(): data_by_color = group[1][group[1][color_name] == color_val] - trace = graph_objs.Scatter( + trace = dict( x=data_by_color[x], y=data_by_color[y], - mode='markers', - type=temp_trace, + type=trace_type, name=color_val, marker=dict( color=colormap[color_val], - **kwargs_marker ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict( + color=colormap[group[0]], **kwargs_marker + ) + fig.append_trace(trace, j + 1 if facet_row else 1, 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, if group.values.tolist() != [[None, None, None]]: group_filtered = group[group[color_name] == color_val] - trace = graph_objs.Scatter( + trace = dict( x=group_filtered[x], y=group_filtered[y], - mode='markers', - type=temp_trace, + type=trace_type, name=color_val, marker=dict( color=colormap[color_val], - **kwargs_marker ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict( + color=colormap[color_val], **kwargs_marker + ) + else: - trace = graph_objs.Scatter( + trace = dict( x=group[x], y=group[y], - mode='markers', - type=temp_trace, + type=trace_type, name=color_val, marker=dict( color=colormap[color_val], - **kwargs_marker ), showlegend=False, **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict( + color=colormap[color_val], **kwargs_marker + ) + + fig.append_trace(trace, row_count + 1, col_count + 1) if row_count == 0: 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, def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, - facet_col_labels, temp_trace, + facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker): @@ -323,19 +340,23 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, annotations = [] if not facet_row and not facet_col: - trace = graph_objs.Scatter( + trace = dict( x=df[x], y=df[y], - mode='markers', - type=temp_trace, + type=trace_type, marker=dict( color=df[color_name], colorscale=colormap, showscale=True, - **kwargs_marker ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict( + color=df[color_name], **kwargs_marker + ) + fig.append_trace(trace, 1, 1) if (facet_row and not facet_col) or (not facet_row and facet_col): @@ -343,20 +364,24 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, df.groupby(facet_row if facet_row else facet_col) ) for j, group in enumerate(groups_by_facet): - trace = graph_objs.Scatter( + trace = dict( x=group[1][x], y=group[1][y], - mode='markers', - type=temp_trace, + type=trace_type, marker=dict( color=df[color_name], colorscale=colormap, showscale=True, colorbar=dict(x=1.15), - **kwargs_marker ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict( + color=df[color_name], **kwargs_marker + ) + fig.append_trace( trace, 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, columns=[x, y, color_name]) if group.values.tolist() != [[None, None, None]]: - trace = graph_objs.Scatter( + trace = dict( x=group[x], y=group[y], - mode='markers', - type=temp_trace, + type=trace_type, marker=dict( color=df[color_name], colorscale=colormap, showscale=(row_count == 0), colorbar=dict(x=1.15), - **kwargs_marker ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict( + color=df[color_name], **kwargs_marker + ) + else: - trace = graph_objs.Scatter( + trace = dict( x=group[x], y=group[y], - mode='markers', - type=temp_trace, + type=trace_type, showlegend=False, **kwargs ) + fig.append_trace(trace, row_count + 1, col_count + 1) if row_count == 0: 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, def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, - temp_trace, flipped_rows, flipped_cols, show_boxes, + trace_type, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker): fig = make_subplots(rows=num_of_rows, cols=num_of_cols, @@ -449,17 +478,19 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, vertical_spacing=SUBPLOT_SPACING, print_grid=False) annotations = [] if not facet_row and not facet_col: - trace = graph_objs.Scatter( + trace = dict( x=df[x], y=df[y], - mode='markers', - type=temp_trace, + type=trace_type, marker=dict( color=marker_color, - **kwargs_marker ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict(color=marker_color, **kwargs_marker) + fig.append_trace(trace, 1, 1) elif (facet_row and not facet_col) or (not facet_row and facet_col): @@ -467,17 +498,19 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, df.groupby(facet_row if facet_row else facet_col) ) for j, group in enumerate(groups_by_facet): - trace = graph_objs.Scatter( + trace = dict( x=group[1][x], y=group[1][y], - mode='markers', - type=temp_trace, + type=trace_type, marker=dict( color=marker_color, - **kwargs_marker ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict(color=marker_color, **kwargs_marker) + fig.append_trace(trace, j + 1 if facet_row else 1, 1 if facet_row else j + 1) @@ -511,17 +544,21 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, group = tuple_to_facet_group[(x_val, y_val)] except KeyError: group = pd.DataFrame([[None, None]], columns=[x, y]) - trace = graph_objs.Scatter( + trace = dict( x=group[x], y=group[y], - mode='markers', - type=temp_trace, + type=trace_type, marker=dict( color=marker_color, - **kwargs_marker ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict( + color=marker_color, **kwargs_marker + ) + fig.append_trace(trace, row_count + 1, col_count + 1) if row_count == 0: label = _return_label(col_values[col_count], @@ -720,6 +757,9 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, "x, y, facet_row, facet_col and color_name must be keys " "in your dataframe." ) + # autoscale histogram bars + if trace_type == 'histogram': + scales = 'free' # validate scales 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, "'trace_type' must be in {}".format(VALID_TRACE_TYPES) ) - temp_trace = 'scatter' if (trace_type == 'histogram') else trace_type - # seperate kwargs for marker and else if 'marker' in kwargs: kwargs_marker = kwargs['marker'] @@ -811,7 +849,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_categorical( df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, - temp_trace, flipped_rows, flipped_cols, show_boxes, + trace_type, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -830,7 +868,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_categorical( df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, - facet_col_labels, temp_trace, flipped_rows, + facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -842,7 +880,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_numerical( df, x, y, facet_row, facet_col, color_name, colorscale_list, num_of_rows, num_of_cols, - facet_row_labels, facet_col_labels, temp_trace, + facet_row_labels, facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -858,7 +896,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_numerical( df, x, y, facet_row, facet_col, color_name, colorscale_list, num_of_rows, num_of_cols, - facet_row_labels, facet_col_labels, temp_trace, + facet_row_labels, facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -867,7 +905,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fig = _facet_grid_color_numerical( df, x, y, facet_row, facet_col, color_name, colorscale_list, num_of_rows, num_of_cols, - facet_row_labels, facet_col_labels, temp_trace, + facet_row_labels, facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -875,7 +913,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, else: fig = _facet_grid( df, x, y, facet_row, facet_col, num_of_rows, num_of_cols, - facet_row_labels, facet_col_labels, temp_trace, flipped_rows, + facet_row_labels, facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, marker_color, kwargs_trace, kwargs_marker ) @@ -1015,10 +1053,4 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, if '{}axis'.format(x_y) in key and range_are_numbers: fig['layout'][key]['range'] = [min_range, max_range] - if trace_type == 'histogram': - for trace in fig['data']: - trace['type'] = trace_type - del trace['marker']['size'] - del trace['mode'] - return fig From 37d576d594aa3d7d2c19e733da81ae68b075bd21 Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Wed, 21 Jun 2017 12:47:25 -0400 Subject: [PATCH 04/14] added lines to histograms - like in scatter --- plotly/figure_factory/_facet_grid.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index 86bb6555ed2..835fc615cc2 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -484,6 +484,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, type=trace_type, marker=dict( color=marker_color, + line=kwargs_marker['line'] ), **kwargs_trace ) @@ -504,6 +505,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, type=trace_type, marker=dict( color=marker_color, + line=kwargs_marker['line'] ), **kwargs_trace ) @@ -550,6 +552,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, type=trace_type, marker=dict( color=marker_color, + line=kwargs_marker['line'] ), **kwargs_trace ) From 180e0b715fc2c190f5fe39685526b93953f58570 Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Mon, 26 Jun 2017 13:58:53 -0400 Subject: [PATCH 05/14] fixed scales oversight --- plotly/figure_factory/_facet_grid.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index 835fc615cc2..756c7d13e6c 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -488,6 +488,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, ), **kwargs_trace ) + if trace_type in ['scatter', 'scattergl']: trace['mode'] = 'markers' trace['marker'] = dict(color=marker_color, **kwargs_marker) @@ -770,10 +771,10 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, "'scales' must be set to 'fixed', 'free_x', 'free_y' and 'free'." ) - if trace_type not in VALID_TRACE_TYPES: - raise exceptions.PlotlyError( - "'trace_type' must be in {}".format(VALID_TRACE_TYPES) - ) + #if trace_type not in VALID_TRACE_TYPES: + # raise exceptions.PlotlyError( + # "'trace_type' must be in {}".format(VALID_TRACE_TYPES) + # ) # seperate kwargs for marker and else if 'marker' in kwargs: @@ -988,15 +989,15 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, fixed_axes = ['y'] elif scales == 'free_y': fixed_axes = ['x'] - else: + elif scales == 'free': fixed_axes = [] # fixed ranges - for x_y in ['x', 'y']: + for x_y in fixed_axes: min_ranges = [] max_ranges = [] for trace in fig['data']: - if len(trace[x_y]) > 0: + if trace[x_y] is not None and len(trace[x_y]) > 0: min_ranges.append(min(trace[x_y])) max_ranges.append(max(trace[x_y])) while None in min_ranges: From 483b4876f73f0c5d757fc3cccbd2b09bcd71676d Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Mon, 26 Jun 2017 18:32:10 -0400 Subject: [PATCH 06/14] add several more trace_types --- plotly/figure_factory/_facet_grid.py | 190 +++++++++++++++------------ 1 file changed, 106 insertions(+), 84 deletions(-) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index 756c7d13e6c..5b0a76be7b6 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -25,7 +25,7 @@ THRES_FOR_FLIPPED_FACET_TITLES = 10 GRID_WIDTH = 1 -VALID_TRACE_TYPES = ['scatter', 'scattergl', 'histogram'] +VALID_TRACE_TYPES = ['scatter', 'scattergl', 'histogram', 'bar', 'box'] CUSTOM_LABEL_ERROR = ( "If you are using a dictionary for custom labels for the facet row/col, " @@ -126,6 +126,9 @@ def _axis_title_annotation(text, x_or_y_axis): y_pos = 0.5 textangle = 270 + if not text: + text = '' + annot = {'font': {'color': '#000000', 'size': AXIS_TITLE_SIZE}, 'showarrow': False, 'text': text, @@ -174,12 +177,19 @@ def _add_shapes_to_fig(fig, annot_rect_color, flipped_rows=False, fig['layout']['shapes'].append(shape) +def _make_trace_for_scatter(trace, trace_type, color, **kwargs_marker): + if trace_type in ['scatter', 'scattergl']: + trace['mode'] = 'markers' + trace['marker'] = dict(color=color, **kwargs_marker) + return trace + + def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, - colormap, num_of_rows, - num_of_cols, facet_row_labels, - facet_col_labels, trace_type, - flipped_rows, flipped_cols, show_boxes, - marker_color, kwargs_trace, kwargs_marker): + colormap, num_of_rows, num_of_cols, + facet_row_labels, facet_col_labels, + trace_type, flipped_rows, flipped_cols, + show_boxes, marker_color, kwargs_trace, + kwargs_marker): fig = make_subplots(rows=num_of_rows, cols=num_of_cols, shared_xaxes=True, shared_yaxes=True, @@ -191,8 +201,6 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, color_groups = list(df.groupby(color_name)) for group in color_groups: trace = dict( - x=group[1][x], - y=group[1][y], type=trace_type, name=group[0], marker=dict( @@ -200,11 +208,13 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, ), **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict( - color=colormap[group[0]], **kwargs_marker - ) + if x: + trace['x'] = group[1][x] + if y: + trace['y'] = group[1][y] + trace = _make_trace_for_scatter( + trace, trace_type, colormap[group[0]], **kwargs_marker + ) fig.append_trace(trace, 1, 1) @@ -216,8 +226,6 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, for color_val in df[color_name].unique(): data_by_color = group[1][group[1][color_name] == color_val] trace = dict( - x=data_by_color[x], - y=data_by_color[y], type=trace_type, name=color_val, marker=dict( @@ -225,11 +233,13 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, ), **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict( - color=colormap[group[0]], **kwargs_marker - ) + if x: + trace['x'] = data_by_color[x] + if y: + trace['y'] = data_by_color[y] + trace = _make_trace_for_scatter( + trace, trace_type, colormap[color_val], **kwargs_marker + ) fig.append_trace(trace, j + 1 if facet_row else 1, @@ -271,8 +281,6 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, group_filtered = group[group[color_name] == color_val] trace = dict( - x=group_filtered[x], - y=group_filtered[y], type=trace_type, name=color_val, marker=dict( @@ -280,16 +288,10 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, ), **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict( - color=colormap[color_val], **kwargs_marker - ) - + new_x = group_filtered[x] + new_y = group_filtered[y] else: trace = dict( - x=group[x], - y=group[y], type=trace_type, name=color_val, marker=dict( @@ -298,12 +300,17 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, showlegend=False, **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict( - color=colormap[color_val], **kwargs_marker - ) - + new_x = group[x] + new_y = group[y] + + if x: + trace['x'] = new_x + if y: + trace['y'] = new_y + trace = _make_trace_for_scatter( + trace, trace_type, colormap[color_val], + **kwargs_marker + ) fig.append_trace(trace, row_count + 1, col_count + 1) if row_count == 0: @@ -341,8 +348,6 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, annotations = [] if not facet_row and not facet_col: trace = dict( - x=df[x], - y=df[y], type=trace_type, marker=dict( color=df[color_name], @@ -351,11 +356,13 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, ), **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict( - color=df[color_name], **kwargs_marker - ) + if x: + trace['x'] = df[x] + if y: + trace['y'] = df[y] + trace = _make_trace_for_scatter( + trace, trace_type, df[color_name], **kwargs_marker + ) fig.append_trace(trace, 1, 1) @@ -365,8 +372,6 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, ) for j, group in enumerate(groups_by_facet): trace = dict( - x=group[1][x], - y=group[1][y], type=trace_type, marker=dict( color=df[color_name], @@ -376,11 +381,13 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, ), **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict( - color=df[color_name], **kwargs_marker - ) + if x: + trace['x'] = group[1][x] + if y: + trace['y'] = group[1][y] + trace = _make_trace_for_scatter( + trace, trace_type, df[color_name], **kwargs_marker + ) fig.append_trace( trace, @@ -419,8 +426,6 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, if group.values.tolist() != [[None, None, None]]: trace = dict( - x=group[x], - y=group[y], type=trace_type, marker=dict( color=df[color_name], @@ -430,21 +435,22 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, ), **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict( - color=df[color_name], **kwargs_marker - ) else: trace = dict( - x=group[x], - y=group[y], type=trace_type, showlegend=False, **kwargs ) + if x: + trace['x'] = group[x] + if y: + trace['y'] = group[y] + trace = _make_trace_for_scatter( + trace, trace_type, df[color_name], **kwargs_marker + ) + fig.append_trace(trace, row_count + 1, col_count + 1) if row_count == 0: label = _return_label(col_values[col_count], @@ -479,8 +485,6 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, annotations = [] if not facet_row and not facet_col: trace = dict( - x=df[x], - y=df[y], type=trace_type, marker=dict( color=marker_color, @@ -489,9 +493,13 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict(color=marker_color, **kwargs_marker) + if x: + trace['x'] = df[x] + if y: + trace['y'] = df[y] + trace = _make_trace_for_scatter( + trace, trace_type, marker_color, **kwargs_marker + ) fig.append_trace(trace, 1, 1) @@ -501,8 +509,6 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, ) for j, group in enumerate(groups_by_facet): trace = dict( - x=group[1][x], - y=group[1][y], type=trace_type, marker=dict( color=marker_color, @@ -510,9 +516,14 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, ), **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict(color=marker_color, **kwargs_marker) + + if x: + trace['x'] = group[1][x] + if y: + trace['y'] = group[1][y] + trace = _make_trace_for_scatter( + trace, trace_type, marker_color, **kwargs_marker + ) fig.append_trace(trace, j + 1 if facet_row else 1, @@ -548,8 +559,6 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, except KeyError: group = pd.DataFrame([[None, None]], columns=[x, y]) trace = dict( - x=group[x], - y=group[y], type=trace_type, marker=dict( color=marker_color, @@ -557,11 +566,13 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, ), **kwargs_trace ) - if trace_type in ['scatter', 'scattergl']: - trace['mode'] = 'markers' - trace['marker'] = dict( - color=marker_color, **kwargs_marker - ) + if x: + trace['x'] = group[x] + if y: + trace['y'] = group[y] + trace = _make_trace_for_scatter( + trace, trace_type, marker_color, **kwargs_marker + ) fig.append_trace(trace, row_count + 1, col_count + 1) if row_count == 0: @@ -587,7 +598,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, return fig -def create_facet_grid(df, x, y, facet_row=None, facet_col=None, +def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, color_name=None, colormap=None, color_is_cat=False, facet_row_labels=None, facet_col_labels=None, height=None, width=None, trace_type='scatter', @@ -752,6 +763,13 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, # make sure all columns are of homogenous datatype utils.validate_dataframe(df) + if trace_type in ['scatter', 'scattergl']: + if not x or not y: + raise exceptions.PlotlyError( + "You need to input 'x' and 'y' if you are you are using a " + "trace_type of 'scatter' or 'scattergl'." + ) + for key in [x, y, facet_row, facet_col, color_name]: if key is not None: try: @@ -762,7 +780,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, "in your dataframe." ) # autoscale histogram bars - if trace_type == 'histogram': + if trace_type not in ['scatter', 'scattergl']: scales = 'free' # validate scales @@ -771,10 +789,10 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, "'scales' must be set to 'fixed', 'free_x', 'free_y' and 'free'." ) - #if trace_type not in VALID_TRACE_TYPES: - # raise exceptions.PlotlyError( - # "'trace_type' must be in {}".format(VALID_TRACE_TYPES) - # ) + if trace_type not in VALID_TRACE_TYPES: + raise exceptions.PlotlyError( + "'trace_type' must be in {}".format(VALID_TRACE_TYPES) + ) # seperate kwargs for marker and else if 'marker' in kwargs: @@ -792,13 +810,17 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, else: kwargs_marker['size'] = 8 + if 'opacity' not in kwargs_marker: + if not ggplot2: + kwargs_marker['opacity'] = 0.75 + if 'line' not in kwargs_marker: if not ggplot2: kwargs_marker['line'] = {'color': 'darkgrey', 'width': 1} # default marker size if not ggplot2: - marker_color = 'rgba(31, 119, 180, 0.5)' + marker_color = 'rgb(31, 119, 180)' else: marker_color = 'rgb(0, 0, 0)' @@ -969,7 +991,7 @@ def create_facet_grid(df, x, y, facet_row=None, facet_col=None, axis_labels['y'].append(key) string_number_in_data = False - for var in [x, y]: + for var in [v for v in [x, y] if v]: if isinstance(df[var].tolist()[0], str): for item in df[var]: try: From 5273f2d86fda99f7ba5debbc1e68b5130c1f3173 Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Mon, 26 Jun 2017 18:37:54 -0400 Subject: [PATCH 07/14] foxed ggplot2 line bug --- plotly/figure_factory/_facet_grid.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index 5b0a76be7b6..b8b046d24bb 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -817,6 +817,8 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, if 'line' not in kwargs_marker: if not ggplot2: kwargs_marker['line'] = {'color': 'darkgrey', 'width': 1} + else: + kwargs_marker['line'] = {} # default marker size if not ggplot2: From af965d07c04f664fbaddbefba0b59ff57d4592fe Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Mon, 26 Jun 2017 21:46:12 -0400 Subject: [PATCH 08/14] fixed tests --- plotly/figure_factory/_facet_grid.py | 11 +++++++---- .../test_optional/test_figure_factory.py | 19 +++++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index b8b046d24bb..9dba547d3eb 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -488,7 +488,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, type=trace_type, marker=dict( color=marker_color, - line=kwargs_marker['line'] + line=kwargs_marker['line'], + #opacity=kwargs_marker['opacity'], ), **kwargs_trace ) @@ -512,7 +513,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, type=trace_type, marker=dict( color=marker_color, - line=kwargs_marker['line'] + line=kwargs_marker['line'], + #opacity=kwargs_marker['opacity'], ), **kwargs_trace ) @@ -562,7 +564,8 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, type=trace_type, marker=dict( color=marker_color, - line=kwargs_marker['line'] + line=kwargs_marker['line'], + #opacity=kwargs_marker['opacity'], ), **kwargs_trace ) @@ -812,7 +815,7 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, if 'opacity' not in kwargs_marker: if not ggplot2: - kwargs_marker['opacity'] = 0.75 + kwargs_trace['opacity'] = 0.6 if 'line' not in kwargs_marker: if not ggplot2: diff --git a/plotly/tests/test_optional/test_figure_factory.py b/plotly/tests/test_optional/test_figure_factory.py index 34fc1d4117a..855d5ce2e30 100644 --- a/plotly/tests/test_optional/test_figure_factory.py +++ b/plotly/tests/test_optional/test_figure_factory.py @@ -1935,6 +1935,19 @@ def test_data_must_be_dataframe(self): ff.create_facet_grid, data, 'a', 'b') + def test_x_and_y_for_scatter(self): + data = pd.DataFrame([[0, 0], [1, 1]], columns=['a', 'b']) + + pattern = ( + "You need to input 'x' and 'y' if you are you are using a " + "trace_type of 'scatter' or 'scattergl'." + ) + + self.assertRaisesRegexp(PlotlyError, pattern, + ff.create_facet_grid, + data, 'a') + + def test_valid_col_selection(self): data = pd.DataFrame([[0, 0], [1, 1]], columns=['a', 'b']) @@ -2017,10 +2030,11 @@ def test_valid_facet_grid_fig(self): ) exp_facet_grid = { - 'data': [{'marker': {'color': 'rgba(31, 119, 180, 0.5)', + 'data': [{'marker': {'color': 'rgb(31, 119, 180)', 'line': {'color': 'darkgrey', 'width': 1}, 'size': 8}, 'mode': 'markers', + 'opacity': 0.6, 'type': 'scatter', 'x': [1.8, 1.8, @@ -2038,10 +2052,11 @@ def test_valid_facet_grid_fig(self): 16, 20], 'yaxis': 'y1'}, - {'marker': {'color': 'rgba(31, 119, 180, 0.5)', + {'marker': {'color': 'rgb(31, 119, 180)', 'line': {'color': 'darkgrey', 'width': 1}, 'size': 8}, 'mode': 'markers', + 'opacity': 0.6, 'type': 'scatter', 'x': [2.8, 2.8, From 788dcf9f223becb6f62bcb288b9c7e8bf78c1b6d Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Thu, 29 Jun 2017 12:36:34 -0400 Subject: [PATCH 09/14] string doc and CHANGELOG update --- CHANGELOG.md | 6 ++---- plotly/figure_factory/_facet_grid.py | 25 ++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f0c20af2a2..313e4dddcb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,14 +2,12 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -## [Unreleased] -### Added -- `figure_factory.create_facet_grid` now supports histogram traces. - ## [2.0.12] - UNRELEASED ### Updated - Updated `plotly.min.js` to version 1.28.2 for `plotly.offline`. - See [the plotly.js CHANGELOG](https://github.com/plotly/plotly.js/blob/master/CHANGELOG.md#1282----2017-06-21) for additional information regarding the updates. +### Added +- `figure_factory.create_facet_grid` now supports histogram, bar, and box traces. ## [2.0.11] - 2017-06-20 ### Updated diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index 9dba547d3eb..17aef767ed0 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -637,7 +637,8 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, :param (int) height: the height of the facet grid figure. :param (int) width: the width of the facet grid figure. :param (str) trace_type: decides the type of plot to appear in the - facet grid. The options are 'scatter', 'scattergl' and 'histogram'. + facet grid. The options are 'scatter', 'scattergl', 'histogram', + 'bar', and 'box'. Default = 'scatter'. :param (str) scales: determines if axes have fixed ranges or not. Valid settings are 'fixed' (all axes fixed), 'free_x' (x axis free only), @@ -668,7 +669,6 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, y='cty', facet_col='cyl', ) - py.iplot(fig, filename='facet_grid_mpg_one_way_facet') ``` @@ -688,7 +688,6 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, facet_row='drv', facet_col='cyl', ) - py.iplot(fig, filename='facet_grid_mpg_two_way_facet') ``` @@ -752,6 +751,26 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, py.iplot(fig, filename='facet_grid_mtcars_custom_labels') ``` + + Example 6: Other Trace Type + ``` + import plotly.plotly as py + import plotly.figure_factory as ff + + import pandas as pd + + mtcars = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/mtcars.csv') + + fig = ff.create_facet_grid( + mtcars, + x='wt', + y='mpg', + facet_col='cyl', + trace_type='histogram', + ) + + py.iplot(fig, filename='facet_grid_mtcars_other_trace_type') + ``` """ if not pd: raise exceptions.ImportError( From e532d3e67ede262fe08770b5a86855a99b250de1 Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Thu, 29 Jun 2017 15:52:48 -0400 Subject: [PATCH 10/14] patched color - works now --- plotly/figure_factory/_facet_grid.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index 17aef767ed0..e5c29fc5963 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -5,8 +5,9 @@ from plotly.graph_objs import graph_objs from plotly.tools import make_subplots -from numbers import Number import math +import copy +from numbers import Number pd = optional_imports.get_module('pandas') @@ -489,7 +490,6 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, marker=dict( color=marker_color, line=kwargs_marker['line'], - #opacity=kwargs_marker['opacity'], ), **kwargs_trace ) @@ -514,7 +514,6 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, marker=dict( color=marker_color, line=kwargs_marker['line'], - #opacity=kwargs_marker['opacity'], ), **kwargs_trace ) @@ -565,7 +564,6 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, marker=dict( color=marker_color, line=kwargs_marker['line'], - #opacity=kwargs_marker['opacity'], ), **kwargs_trace ) @@ -818,14 +816,15 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, # seperate kwargs for marker and else if 'marker' in kwargs: - kwargs_marker = kwargs['marker'] + kwargs_marker = copy.copy(kwargs['marker']) else: kwargs_marker = {} - for param in ['color']: - kwargs_marker.pop(param, None) + marker_color = kwargs_marker.pop('color', None) kwargs.pop('marker', None) - kwargs_trace = kwargs + kwargs_trace = copy.copy(kwargs) + print kwargs_marker + print kwargs_trace if 'size' not in kwargs_marker: if ggplot2: kwargs_marker['size'] = 5 @@ -844,7 +843,8 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, # default marker size if not ggplot2: - marker_color = 'rgb(31, 119, 180)' + if not marker_color: + marker_color = 'rgb(31, 119, 180)' else: marker_color = 'rgb(0, 0, 0)' From 8cd0de17f07c24d21a7805c3fe96dc0ff9a6187d Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Thu, 29 Jun 2017 17:04:20 -0400 Subject: [PATCH 11/14] fix spacing for histogram --- plotly/figure_factory/_facet_grid.py | 59 ++++++++++++++++------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index e5c29fc5963..cf96ad69191 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -18,7 +18,6 @@ LEGEND_COLOR = '#efefef' PLOT_BGCOLOR = '#ededed' ANNOT_RECT_COLOR = '#d0d0d0' -SUBPLOT_SPACING = 0.015 LEGEND_BORDER_WIDTH = 1 LEGEND_ANNOT_X = 1.05 LEGEND_ANNOT_Y = 0.5 @@ -72,7 +71,8 @@ def _legend_annotation(color_name): return legend_title -def _annotation_dict(text, lane, num_of_lanes, row_col='col', flipped=True): +def _annotation_dict(text, lane, num_of_lanes, SUBPLOT_SPACING, row_col='col', + flipped=True): l = (1 - (num_of_lanes - 1) * SUBPLOT_SPACING) / (num_of_lanes) if not flipped: xanchor = 'center' @@ -189,8 +189,8 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, trace_type, flipped_rows, flipped_cols, - show_boxes, marker_color, kwargs_trace, - kwargs_marker): + show_boxes, SUBPLOT_SPACING, marker_color, + kwargs_trace, kwargs_marker): fig = make_subplots(rows=num_of_rows, cols=num_of_cols, shared_xaxes=True, shared_yaxes=True, @@ -257,6 +257,7 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, label, num_of_rows - j if facet_row else j + 1, num_of_rows if facet_row else num_of_cols, + SUBPLOT_SPACING, 'row' if facet_row else 'col', flipped_rows) ) @@ -319,12 +320,14 @@ def _facet_grid_color_categorical(df, x, y, facet_row, facet_col, color_name, facet_col_labels, facet_col) annotations.append( _annotation_dict(label, col_count + 1, num_of_cols, + SUBPLOT_SPACING, row_col='col', flipped=flipped_cols) ) label = _return_label(row_values[row_count], facet_row_labels, facet_row) annotations.append( _annotation_dict(label, num_of_rows - row_count, num_of_rows, + SUBPLOT_SPACING, row_col='row', flipped=flipped_rows) ) @@ -339,7 +342,8 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, num_of_cols, facet_row_labels, facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, - marker_color, kwargs_trace, kwargs_marker): + SUBPLOT_SPACING, marker_color, kwargs_trace, + kwargs_marker): fig = make_subplots(rows=num_of_rows, cols=num_of_cols, shared_xaxes=True, shared_yaxes=True, @@ -406,6 +410,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, label, num_of_rows - j if facet_row else j + 1, num_of_rows if facet_row else num_of_cols, + SUBPLOT_SPACING, 'row' if facet_row else 'col', flipped=flipped_rows) ) @@ -458,13 +463,14 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, facet_col_labels, facet_col) annotations.append( _annotation_dict(label, col_count + 1, num_of_cols, + SUBPLOT_SPACING, row_col='col', flipped=flipped_cols) ) label = _return_label(row_values[row_count], facet_row_labels, facet_row) annotations.append( _annotation_dict(row_values[row_count], - num_of_rows - row_count, num_of_rows, + num_of_rows - row_count, num_of_rows, SUBPLOT_SPACING, row_col='row', flipped=flipped_rows) ) @@ -477,7 +483,7 @@ def _facet_grid_color_numerical(df, x, y, facet_row, facet_col, color_name, def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, - marker_color, kwargs_trace, kwargs_marker): + SUBPLOT_SPACING, marker_color, kwargs_trace, kwargs_marker): fig = make_subplots(rows=num_of_rows, cols=num_of_cols, shared_xaxes=True, shared_yaxes=True, @@ -541,6 +547,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, label, num_of_rows - j if facet_row else j + 1, num_of_rows if facet_row else num_of_cols, + SUBPLOT_SPACING, 'row' if facet_row else 'col', flipped_rows ) @@ -581,7 +588,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, facet_col_labels, facet_col) annotations.append( - _annotation_dict(label, col_count + 1, num_of_cols, + _annotation_dict(label, col_count + 1, num_of_cols, SUBPLOT_SPACING, row_col='col', flipped=flipped_cols) ) @@ -589,7 +596,7 @@ def _facet_grid(df, x, y, facet_row, facet_col, num_of_rows, facet_row_labels, facet_row) annotations.append( - _annotation_dict(label, num_of_rows - row_count, num_of_rows, + _annotation_dict(label, num_of_rows - row_count, num_of_rows, SUBPLOT_SPACING, row_col='row', flipped=flipped_rows) ) @@ -762,7 +769,6 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, fig = ff.create_facet_grid( mtcars, x='wt', - y='mpg', facet_col='cyl', trace_type='histogram', ) @@ -814,17 +820,20 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, "'trace_type' must be in {}".format(VALID_TRACE_TYPES) ) + if trace_type == 'histogram': + SUBPLOT_SPACING = 0.06 + else: + SUBPLOT_SPACING = 0.015 + # seperate kwargs for marker and else if 'marker' in kwargs: - kwargs_marker = copy.copy(kwargs['marker']) + kwargs_marker = kwargs['marker'] else: kwargs_marker = {} marker_color = kwargs_marker.pop('color', None) kwargs.pop('marker', None) - kwargs_trace = copy.copy(kwargs) + kwargs_trace = kwargs - print kwargs_marker - print kwargs_trace if 'size' not in kwargs_marker: if ggplot2: kwargs_marker['size'] = 5 @@ -900,7 +909,7 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, trace_type, flipped_rows, flipped_cols, show_boxes, - marker_color, kwargs_trace, kwargs_marker + SUBPLOT_SPACING, marker_color, kwargs_trace, kwargs_marker ) elif isinstance(df[color_name][0], Number): @@ -919,8 +928,8 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, df, x, y, facet_row, facet_col, color_name, colormap, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, trace_type, flipped_rows, - flipped_cols, show_boxes, marker_color, kwargs_trace, - kwargs_marker + flipped_cols, show_boxes, SUBPLOT_SPACING, marker_color, + kwargs_trace, kwargs_marker ) elif isinstance(colormap, list): @@ -931,8 +940,8 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, df, x, y, facet_row, facet_col, color_name, colorscale_list, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, trace_type, - flipped_rows, flipped_cols, show_boxes, marker_color, - kwargs_trace, kwargs_marker + flipped_rows, flipped_cols, show_boxes, SUBPLOT_SPACING, + marker_color, kwargs_trace, kwargs_marker ) elif isinstance(colormap, str): if colormap in colors.PLOTLY_SCALES.keys(): @@ -947,8 +956,8 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, df, x, y, facet_row, facet_col, color_name, colorscale_list, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, trace_type, - flipped_rows, flipped_cols, show_boxes, marker_color, - kwargs_trace, kwargs_marker + flipped_rows, flipped_cols, show_boxes, SUBPLOT_SPACING, + marker_color, kwargs_trace, kwargs_marker ) else: colorscale_list = colors.PLOTLY_SCALES['Reds'] @@ -956,16 +965,16 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, df, x, y, facet_row, facet_col, color_name, colorscale_list, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, trace_type, - flipped_rows, flipped_cols, show_boxes, marker_color, - kwargs_trace, kwargs_marker + flipped_rows, flipped_cols, show_boxes, SUBPLOT_SPACING, + marker_color, kwargs_trace, kwargs_marker ) else: fig = _facet_grid( df, x, y, facet_row, facet_col, num_of_rows, num_of_cols, facet_row_labels, facet_col_labels, trace_type, flipped_rows, - flipped_cols, show_boxes, marker_color, kwargs_trace, - kwargs_marker + flipped_cols, show_boxes, SUBPLOT_SPACING, marker_color, + kwargs_trace, kwargs_marker ) if not height: From 1ff4b5ffbffbfd36bf141072640a8b6476756f54 Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Thu, 29 Jun 2017 17:14:23 -0400 Subject: [PATCH 12/14] null --- plotly/figure_factory/_facet_grid.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index cf96ad69191..662e2cc3fdf 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -815,10 +815,10 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, "'scales' must be set to 'fixed', 'free_x', 'free_y' and 'free'." ) - if trace_type not in VALID_TRACE_TYPES: - raise exceptions.PlotlyError( - "'trace_type' must be in {}".format(VALID_TRACE_TYPES) - ) + #if trace_type not in VALID_TRACE_TYPES: + # raise exceptions.PlotlyError( + # "'trace_type' must be in {}".format(VALID_TRACE_TYPES) + # ) if trace_type == 'histogram': SUBPLOT_SPACING = 0.06 From c99c0e7d052b46e8a793e1d5a7b3d67ca31edd86 Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Thu, 29 Jun 2017 17:16:01 -0400 Subject: [PATCH 13/14] trace types --- plotly/figure_factory/_facet_grid.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plotly/figure_factory/_facet_grid.py b/plotly/figure_factory/_facet_grid.py index 662e2cc3fdf..cf96ad69191 100644 --- a/plotly/figure_factory/_facet_grid.py +++ b/plotly/figure_factory/_facet_grid.py @@ -815,10 +815,10 @@ def create_facet_grid(df, x=None, y=None, facet_row=None, facet_col=None, "'scales' must be set to 'fixed', 'free_x', 'free_y' and 'free'." ) - #if trace_type not in VALID_TRACE_TYPES: - # raise exceptions.PlotlyError( - # "'trace_type' must be in {}".format(VALID_TRACE_TYPES) - # ) + if trace_type not in VALID_TRACE_TYPES: + raise exceptions.PlotlyError( + "'trace_type' must be in {}".format(VALID_TRACE_TYPES) + ) if trace_type == 'histogram': SUBPLOT_SPACING = 0.06 From 1b0e4344870cbe6d7558a94c6225b95233112ce4 Mon Sep 17 00:00:00 2001 From: Adam Kulidjian Date: Fri, 30 Jun 2017 15:52:36 -0400 Subject: [PATCH 14/14] date to changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e93444cdc3e..5130cee5b62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -## [2.0.12] - UNRELEASED +## [2.0.12] - 2017-06-30 ### Updated - Updated `plotly.min.js` to version 1.28.3 for `plotly.offline`. - See [the plotly.js CHANGELOG](https://github.com/plotly/plotly.js/blob/master/CHANGELOG.md#1283----2017-06-26) for additional information regarding the updates.