Skip to content

added histogram to facet_grid #776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jul 1, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe figure_factory.create_facet_grid just to be super clear


## [2.0.10] - 2017-06-12
### Added
- The figure_factory can now create facet grids with `.create_facet_grid`. Check it out with:
Expand Down
55 changes: 32 additions & 23 deletions plotly/figure_factory/_facet_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
THRES_FOR_FLIPPED_FACET_TITLES = 10
GRID_WIDTH = 1

VALID_TRACE_TYPES = ['scatter', 'scattergl', 'histogram']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically other types too though, right? I think that box, histogram2d, and bar might work now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember not liking the look of box or bar - it was very squashed up, but perhaps it was the dataset


CUSTOM_LABEL_ERROR = (
"If you are using a dictionary for custom labels for the facet row/col, "
Expand Down Expand Up @@ -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):

Expand All @@ -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],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having a hard time understanding what happens when the histogram mode sets both x and y here. Shouldn't only one of them be set?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is just the other variable ignored?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, or the user just doesn't supply the y variable when setting it? Like

FF.facet_grid(df, x='categorial-column', facet_col='another-column', trace_type='histogram')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You found an oversight in my design.

Originally this was meant for just scatter, so I was forcing x and y. I'm going to try not forcing x and y and putting df[x] and df[y] in the trace dictionary depending on what the user provides

mode='markers',
type=trace_type,
type=temp_trace,
name=group[0],
marker=dict(
color=colormap[group[0]],
Expand All @@ -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],
Expand Down Expand Up @@ -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],
Expand All @@ -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],
Expand Down Expand Up @@ -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):

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
)
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this renders the trace as scatter first, the histogram attributes can't be passed. For example:

tips = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/tips.csv')

fig = ff.create_facet_grid(
    tips,
    x='total_bill',
    y='tip',
    trace_type='histogram',
    histfunc='sum',
    facet_row='sex',
    facet_col='smoker'
)
py.iplot(fig, filename='facet_grid_tips_sequential_colors')

fails with:
screen shot 2017-06-20 at 2 08 42 pm


# seperate kwargs for marker and else
if 'marker' in kwargs:
kwargs_marker = kwargs['marker']
Expand Down Expand Up @@ -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
)

Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -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
)
Expand All @@ -864,15 +867,15 @@ 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
)

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
)
Expand Down Expand Up @@ -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
7 changes: 2 additions & 5 deletions plotly/tests/test_optional/test_figure_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down