|
9 | 9 | import numpy as np
|
10 | 10 |
|
11 | 11 |
|
| 12 | +class TestDistplot(TestCase): |
| 13 | + |
| 14 | + def test_wrong_curve_type(self): |
| 15 | + |
| 16 | + # check: PlotlyError (and specific message) is raised if curve_type is |
| 17 | + # not 'kde' or 'normal' |
| 18 | + |
| 19 | + kwargs = {'hist_data': [[1, 2, 3]], 'group_labels': ['group'], |
| 20 | + 'curve_type': 'curve'} |
| 21 | + self.assertRaisesRegexp(PlotlyError, "curve_type must be defined as " |
| 22 | + "'kde' or 'normal'", |
| 23 | + tls.FigureFactory.create_distplot, **kwargs) |
| 24 | + |
| 25 | + def test_wrong_histdata_format(self): |
| 26 | + |
| 27 | + # check: PlotlyError if hist_data is not a list of lists or list of |
| 28 | + # np.ndarrays (if hist_data is entered as just a list the function |
| 29 | + # will fail) |
| 30 | + |
| 31 | + kwargs = {'hist_data': [1, 2, 3], 'group_labels': ['group']} |
| 32 | + self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot, |
| 33 | + **kwargs) |
| 34 | + |
| 35 | + def test_unequal_data_label_length(self): |
| 36 | + kwargs = {'hist_data': [[1, 2]], 'group_labels': ['group', 'group2']} |
| 37 | + self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot, |
| 38 | + **kwargs) |
| 39 | + |
| 40 | + kwargs = {'hist_data': [[1, 2], [1, 2, 3]], 'group_labels': ['group']} |
| 41 | + self.assertRaises(PlotlyError, tls.FigureFactory.create_distplot, |
| 42 | + **kwargs) |
| 43 | + |
| 44 | + def test_simple_distplot(self): |
| 45 | + |
| 46 | + # we should be able to create a single distplot with a simple dataset |
| 47 | + # and default kwargs |
| 48 | + |
| 49 | + dp = tls.FigureFactory.create_distplot(hist_data=[[1, 2, 2, 3]], |
| 50 | + group_labels=['distplot']) |
| 51 | + expected_dp_layout = {'barmode': 'overlay', |
| 52 | + 'hovermode': 'closest', |
| 53 | + 'legend': {'traceorder': 'reversed'}, |
| 54 | + 'xaxis1': {'anchor': 'y2', 'domain': [0.0, 1.0], 'zeroline': False}, |
| 55 | + 'yaxis1': {'anchor': 'free', 'domain': [0.35, 1], 'position': 0.0}, |
| 56 | + 'yaxis2': {'anchor': 'x1', |
| 57 | + 'domain': [0, 0.25], |
| 58 | + 'dtick': 1, |
| 59 | + 'showticklabels': False}} |
| 60 | + self.assertEqual(dp['layout'], expected_dp_layout) |
| 61 | + |
| 62 | + expected_dp_data_hist = {'autobinx': False, |
| 63 | + 'histnorm': 'probability', |
| 64 | + 'legendgroup': 'distplot', |
| 65 | + 'marker': {'color': 'rgb(31, 119, 180)'}, |
| 66 | + 'name': 'distplot', |
| 67 | + 'opacity': 0.7, |
| 68 | + 'type': 'histogram', |
| 69 | + 'x': [1, 2, 2, 3], |
| 70 | + 'xaxis': 'x1', |
| 71 | + 'xbins': {'end': 3.0, 'size': 1.0, 'start': 1.0}, |
| 72 | + 'yaxis': 'y1'} |
| 73 | + self.assertEqual(dp['data'][0], expected_dp_data_hist) |
| 74 | + |
| 75 | + expected_dp_data_rug = {'legendgroup': 'distplot', |
| 76 | + 'marker': {'color': 'rgb(31, 119, 180)', |
| 77 | + 'symbol': 'line-ns-open'}, |
| 78 | + 'mode': 'markers', |
| 79 | + 'name': 'distplot', |
| 80 | + 'showlegend': False, |
| 81 | + 'text': None, |
| 82 | + 'type': 'scatter', |
| 83 | + 'x': [1, 2, 2, 3], |
| 84 | + 'xaxis': 'x1', |
| 85 | + 'y': ['distplot', 'distplot', |
| 86 | + 'distplot', 'distplot'], |
| 87 | + 'yaxis': 'y2'} |
| 88 | + self.assertEqual(dp['data'][2], expected_dp_data_rug) |
| 89 | + |
| 90 | + def test_distplot_more_args(self): |
| 91 | + |
| 92 | + # we should be able to create a distplot with 2 datasets no |
| 93 | + # rugplot, defined bin_size, and added title |
| 94 | + |
| 95 | + hist1_x = [0.8, 1.2, 0.2, 0.6, 1.6, |
| 96 | + -0.9, -0.07, 1.95, 0.9, -0.2, |
| 97 | + -0.5, 0.3, 0.4, -0.37, 0.6] |
| 98 | + hist2_x = [0.8, 1.5, 1.5, 0.6, 0.59, |
| 99 | + 1.0, 0.8, 1.7, 0.5, 0.8, |
| 100 | + -0.3, 1.2, 0.56, 0.3, 2.2] |
| 101 | + |
| 102 | + hist_data = [hist1_x] + [hist2_x] |
| 103 | + group_labels = ['2012', '2013'] |
| 104 | + |
| 105 | + dp = tls.FigureFactory.create_distplot(hist_data, group_labels, |
| 106 | + show_rug=False, bin_size=.2) |
| 107 | + dp['layout'].update(title='Dist Plot') |
| 108 | + |
| 109 | + expected_dp_layout = {'barmode': 'overlay', |
| 110 | + 'hovermode': 'closest', |
| 111 | + 'legend': {'traceorder': 'reversed'}, |
| 112 | + 'title': 'Dist Plot', |
| 113 | + 'xaxis1': {'anchor': 'y2', 'domain': [0.0, 1.0], |
| 114 | + 'zeroline': False}, |
| 115 | + 'yaxis1': {'anchor': 'free', 'domain': [0.0, 1], |
| 116 | + 'position': 0.0}} |
| 117 | + self.assertEqual(dp['layout'], expected_dp_layout) |
| 118 | + |
| 119 | + expected_dp_data_hist_1 = {'autobinx': False, |
| 120 | + 'histnorm': 'probability', |
| 121 | + 'legendgroup': '2012', |
| 122 | + 'marker': {'color': 'rgb(31, 119, 180)'}, |
| 123 | + 'name': '2012', |
| 124 | + 'opacity': 0.7, |
| 125 | + 'type': 'histogram', |
| 126 | + 'x': [0.8, 1.2, 0.2, 0.6, 1.6, -0.9, -0.07, |
| 127 | + 1.95, 0.9, -0.2, -0.5, 0.3, 0.4, |
| 128 | + -0.37, 0.6], |
| 129 | + 'xaxis': 'x1', |
| 130 | + 'xbins': {'end': 1.95, 'size': 0.2, |
| 131 | + 'start': -0.9}, |
| 132 | + 'yaxis': 'y1'} |
| 133 | + self.assertEqual(dp['data'][0], expected_dp_data_hist_1) |
| 134 | + |
| 135 | + expected_dp_data_hist_2 = {'autobinx': False, |
| 136 | + 'histnorm': 'probability', |
| 137 | + 'legendgroup': '2013', |
| 138 | + 'marker': {'color': 'rgb(255, 127, 14)'}, |
| 139 | + 'name': '2013', |
| 140 | + 'opacity': 0.7, |
| 141 | + 'type': 'histogram', |
| 142 | + 'x': [0.8, 1.5, 1.5, 0.6, 0.59, 1.0, 0.8, |
| 143 | + 1.7, 0.5, 0.8, -0.3, 1.2, 0.56, 0.3, |
| 144 | + 2.2], |
| 145 | + 'xaxis': 'x1', |
| 146 | + 'xbins': {'end': 2.2, 'size': 0.2, |
| 147 | + 'start': -0.3}, |
| 148 | + 'yaxis': 'y1'} |
| 149 | + self.assertEqual(dp['data'][1], expected_dp_data_hist_2) |
| 150 | + |
| 151 | + |
12 | 152 | class TestStreamline(TestCase):
|
13 | 153 |
|
14 | 154 | def test_wrong_arrow_scale(self):
|
|
0 commit comments