Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Distplot #275

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 19 commits into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions optional-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ ipython[all]

## pandas deps for some matplotlib functionality ##
pandas

## scipy deps for some FigureFactory functions ##
scipy
19 changes: 19 additions & 0 deletions plotly/tests/test_core/test_tools/test_figure_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,3 +686,22 @@ def test_datetime_candlestick(self):

self.assertEqual(candle, exp_candle)


# class TestDistplot(TestCase):

# def test_scipy_import_error(self):

# # make sure Import Error is raised when _scipy_imported = False

# hist_data = [[1.1, 1.1, 2.5, 3.0, 3.5,
# 3.5, 4.1, 4.4, 4.5, 4.5,
# 5.0, 5.0, 5.2, 5.5, 5.5,
# 5.5, 5.5, 5.5, 6.1, 7.0]]

# group_labels = ['distplot example']

# self.assertRaisesRegexp(ImportError,
# "FigureFactory.create_distplot requires scipy",
# tls.FigureFactory.create_distplot,
# hist_data, group_labels)

140 changes: 140 additions & 0 deletions plotly/tests/test_optional/test_opt_tracefactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,146 @@
import numpy as np


class TestDistplot(TestCase):

def test_wrong_curve_type(self):

# check: PlotlyError (and specific message) is raised if curve_type is
# not 'kde' or 'normal'

kwargs = {'hist_data': [[1, 2, 3]], 'group_labels': ['group'],
'curve_type': 'curve'}
self.assertRaisesRegexp(PlotlyError, "curve_type must be defined as "
"'kde' or 'normal'",
tls.FigureFactory.create_distplot, **kwargs)

def test_wrong_histdata_format(self):

# check: PlotlyError if hist_data is not a list of lists or list of
# np.ndarrays (if hist_data is entered as just a list the function
# will fail)

kwargs = {'hist_data': [1, 2, 3], 'group_labels': ['group']}
self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot,
**kwargs)

def test_unequal_data_label_length(self):
kwargs = {'hist_data': [[1, 2]], 'group_labels': ['group', 'group2']}
self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot,
**kwargs)

kwargs = {'hist_data': [[1, 2], [1, 2, 3]], 'group_labels': ['group']}
self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot,
**kwargs)

def test_simple_distplot(self):

# we should be able to create a single distplot with a simple dataset
# and default kwargs

dp = tls.FigureFactory.create_distplot(hist_data=[[1, 2, 2, 3]],
group_labels=['distplot'])
expected_dp_layout = {'barmode': 'overlay',
'hovermode': 'closest',
'legend': {'traceorder': 'reversed'},
'xaxis1': {'anchor': 'y2', 'domain': [0.0, 1.0], 'zeroline': False},
'yaxis1': {'anchor': 'free', 'domain': [0.35, 1], 'position': 0.0},
'yaxis2': {'anchor': 'x1',
'domain': [0, 0.25],
'dtick': 1,
'showticklabels': False}}
self.assertEqual(dp['layout'], expected_dp_layout)

expected_dp_data_hist = {'autobinx': False,
'histnorm': 'probability',
'legendgroup': 'distplot',
'marker': {'color': 'rgb(31, 119, 180)'},
'name': 'distplot',
'opacity': 0.7,
'type': 'histogram',
'x': [1, 2, 2, 3],
'xaxis': 'x1',
'xbins': {'end': 3.0, 'size': 1.0, 'start': 1.0},
'yaxis': 'y1'}
self.assertEqual(dp['data'][0], expected_dp_data_hist)

expected_dp_data_rug = {'legendgroup': 'distplot',
'marker': {'color': 'rgb(31, 119, 180)',
'symbol': 'line-ns-open'},
'mode': 'markers',
'name': 'distplot',
'showlegend': False,
'text': None,
'type': 'scatter',
'x': [1, 2, 2, 3],
'xaxis': 'x1',
'y': ['distplot', 'distplot',
'distplot', 'distplot'],
'yaxis': 'y2'}
self.assertEqual(dp['data'][2], expected_dp_data_rug)

def test_distplot_more_args(self):

# we should be able to create a distplot with 2 datasets no
# rugplot, defined bin_size, and added title

hist1_x = [0.8, 1.2, 0.2, 0.6, 1.6,
-0.9, -0.07, 1.95, 0.9, -0.2,
-0.5, 0.3, 0.4, -0.37, 0.6]
hist2_x = [0.8, 1.5, 1.5, 0.6, 0.59,
1.0, 0.8, 1.7, 0.5, 0.8,
-0.3, 1.2, 0.56, 0.3, 2.2]

hist_data = [hist1_x] + [hist2_x]
group_labels = ['2012', '2013']

dp = tls.FigureFactory.create_distplot(hist_data, group_labels,
show_rug=False, bin_size=.2)
dp['layout'].update(title='Dist Plot')

expected_dp_layout = {'barmode': 'overlay',
'hovermode': 'closest',
'legend': {'traceorder': 'reversed'},
'title': 'Dist Plot',
'xaxis1': {'anchor': 'y2', 'domain': [0.0, 1.0],
'zeroline': False},
'yaxis1': {'anchor': 'free', 'domain': [0.0, 1],
'position': 0.0}}
self.assertEqual(dp['layout'], expected_dp_layout)

expected_dp_data_hist_1 = {'autobinx': False,
'histnorm': 'probability',
'legendgroup': '2012',
'marker': {'color': 'rgb(31, 119, 180)'},
'name': '2012',
'opacity': 0.7,
'type': 'histogram',
'x': [0.8, 1.2, 0.2, 0.6, 1.6, -0.9, -0.07,
1.95, 0.9, -0.2, -0.5, 0.3, 0.4,
-0.37, 0.6],
'xaxis': 'x1',
'xbins': {'end': 1.95, 'size': 0.2,
'start': -0.9},
'yaxis': 'y1'}
self.assertEqual(dp['data'][0], expected_dp_data_hist_1)

expected_dp_data_hist_2 = {'autobinx': False,
'histnorm': 'probability',
'legendgroup': '2013',
'marker': {'color': 'rgb(255, 127, 14)'},
'name': '2013',
'opacity': 0.7,
'type': 'histogram',
'x': [0.8, 1.5, 1.5, 0.6, 0.59, 1.0, 0.8,
1.7, 0.5, 0.8, -0.3, 1.2, 0.56, 0.3,
2.2],
'xaxis': 'x1',
'xbins': {'end': 2.2, 'size': 0.2,
'start': -0.3},
'yaxis': 'y1'}
self.assertEqual(dp['data'][1], expected_dp_data_hist_2)


Copy link
Contributor

Choose a reason for hiding this comment

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

\o/ tests!

class TestStreamline(TestCase):

def test_wrong_arrow_scale(self):
Expand Down
Loading