-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Adding Scatterplot Matrix to FigureFactory #417
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
Changes from all commits
f72beed
04c8e80
f7ba1c3
70c916d
68c4143
7d43513
89d28f3
c9d8bf7
6698fcd
3ba79c2
08745cd
14a4bfd
bbd02a5
a6a91c2
c3e7755
09d3c5d
bb48595
1957cf2
fbfe495
a87a07b
84fc4c4
f98da6a
f4764b4
e25d392
7a2dffa
b3bf96d
398b8d5
7afe967
718dd71
78608a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
from nose.tools import raises | ||
|
||
import numpy as np | ||
import pandas as pd | ||
|
||
|
||
class TestDistplot(TestCase): | ||
|
@@ -529,3 +530,308 @@ def test_dendrogram_colorscale(self): | |
self.assert_dict_equal(dendro['data'][0], expected_dendro['data'][0]) | ||
self.assert_dict_equal(dendro['data'][1], expected_dendro['data'][1]) | ||
self.assert_dict_equal(dendro['data'][2], expected_dendro['data'][2]) | ||
|
||
|
||
class TestScatterPlotMatrix(NumpyTestUtilsMixin, TestCase): | ||
|
||
def test_dataframe_input(self): | ||
|
||
# check: dataframe is imported | ||
df = 'foo' | ||
|
||
pattern = ( | ||
"Dataframe not inputed. Please use a pandas dataframe to produce " | ||
"a scatterplot matrix." | ||
) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df) | ||
|
||
def test_one_column_dataframe(self): | ||
|
||
# check: dataframe has 1 column or less | ||
df = pd.DataFrame([1, 2, 3]) | ||
|
||
pattern = ( | ||
"Dataframe has only one column. To use the scatterplot matrix, " | ||
"use at least 2 columns." | ||
) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df) | ||
|
||
def test_valid_diag_choice(self): | ||
|
||
# make sure that the diagonal param is valid | ||
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]]) | ||
|
||
self.assertRaises(PlotlyError, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, diag='foo') | ||
|
||
def test_forbidden_params(self): | ||
|
||
# check: the forbidden params of 'marker' in **kwargs | ||
df = pd.DataFrame([[1, 2, 3], [4, 5, 6]]) | ||
|
||
kwargs = {'marker': {'size': 15}} | ||
|
||
pattern = ( | ||
"Your kwargs dictionary cannot include the 'size', 'color' or " | ||
"'colorscale' key words inside the marker dict since 'size' is " | ||
"already an argument of the scatterplot matrix function and both " | ||
"'color' and 'colorscale are set internally." | ||
) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, **kwargs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
def test_valid_index_choice(self): | ||
|
||
# check: index is a column name | ||
df = pd.DataFrame([[1, 2], [3, 4]], columns=['apple', 'pear']) | ||
|
||
pattern = ( | ||
"Make sure you set the index input variable to one of the column " | ||
"names of your dataframe." | ||
) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, index='grape') | ||
|
||
def test_same_data_in_dataframe_columns(self): | ||
|
||
# check: either all numbers or strings in each dataframe column | ||
df = pd.DataFrame([['a', 2], [3, 4]]) | ||
|
||
pattern = ( | ||
"Error in dataframe. Make sure all entries of each column are " | ||
"either numbers or strings." | ||
) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df) | ||
|
||
df = pd.DataFrame([[1, 2], ['a', 4]]) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df) | ||
|
||
def test_same_data_in_index(self): | ||
|
||
# check: either all numbers or strings in index column | ||
df = pd.DataFrame([['a', 2], [3, 4]], columns=['apple', 'pear']) | ||
|
||
pattern = ( | ||
"Error in indexing column. Make sure all entries of each column " | ||
"are all numbers or all strings." | ||
) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, index='apple') | ||
|
||
df = pd.DataFrame([[1, 2], ['a', 4]], columns=['apple', 'pear']) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, index='apple') | ||
|
||
def test_valid_palette(self): | ||
|
||
# check: the palette argument is in a acceptable form | ||
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], | ||
columns=['a', 'b', 'c']) | ||
|
||
self.assertRaisesRegexp(PlotlyError, "You must pick a valid " | ||
"plotly colorscale name.", | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, use_theme=True, index='a', | ||
palette='fake_scale') | ||
|
||
pattern = ( | ||
"The items of 'palette' must be tripets of the form a,b,c or " | ||
"'rgbx,y,z' where a,b,c belong to the interval 0,1 and x,y,z " | ||
"belong to 0,255." | ||
) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, use_theme=True, palette=1, index='c') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/plotly/plotly.py/pull/417/files#r60284049 (all of these can be made a lot more readable) |
||
|
||
def test_valid_endpts(self): | ||
|
||
# check: the endpts is a list or a tuple | ||
df = pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], | ||
columns=['a', 'b', 'c']) | ||
|
||
pattern = ( | ||
"The intervals_endpts argument must be a list or tuple of a " | ||
"sequence of increasing numbers." | ||
) | ||
|
||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, use_theme=True, index='a', | ||
palette='Blues', endpts='foo') | ||
|
||
# check: the endpts are a list of numbers | ||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, use_theme=True, index='a', | ||
palette='Blues', endpts=['a']) | ||
|
||
# check: endpts is a list of INCREASING numbers | ||
self.assertRaisesRegexp(PlotlyError, pattern, | ||
tls.FigureFactory.create_scatterplotmatrix, | ||
df, use_theme=True, index='a', | ||
palette='Blues', endpts=[2, 1]) | ||
|
||
def test_scatter_plot_matrix(self): | ||
|
||
# check if test scatter plot matrix without index or theme matches | ||
# with the expected output | ||
df = pd.DataFrame([[2, 'Apple'], [6, 'Pear'], | ||
[-15, 'Apple'], [5, 'Pear'], | ||
[-2, 'Apple'], [0, 'Apple']], | ||
columns=['Numbers', 'Fruit']) | ||
|
||
test_scatter_plot_matrix = tls.FigureFactory.create_scatterplotmatrix( | ||
df, diag='scatter', height=1000, width=1000, size=13, | ||
title='Scatterplot Matrix', use_theme=False | ||
) | ||
|
||
exp_scatter_plot_matrix = { | ||
'data': [{'marker': {'size': 13}, | ||
'mode': 'markers', | ||
'showlegend': False, | ||
'type': 'scatter', | ||
'x': [2, 6, -15, 5, -2, 0], | ||
'xaxis': 'x1', | ||
'y': [2, 6, -15, 5, -2, 0], | ||
'yaxis': 'y1'}, | ||
{'marker': {'size': 13}, | ||
'mode': 'markers', | ||
'showlegend': False, | ||
'type': 'scatter', | ||
'x': ['Apple', | ||
'Pear', | ||
'Apple', | ||
'Pear', | ||
'Apple', | ||
'Apple'], | ||
'xaxis': 'x2', | ||
'y': [2, 6, -15, 5, -2, 0], | ||
'yaxis': 'y2'}, | ||
{'marker': {'size': 13}, | ||
'mode': 'markers', | ||
'showlegend': False, | ||
'type': 'scatter', | ||
'x': [2, 6, -15, 5, -2, 0], | ||
'xaxis': 'x3', | ||
'y': ['Apple', | ||
'Pear', | ||
'Apple', | ||
'Pear', | ||
'Apple', | ||
'Apple'], | ||
'yaxis': 'y3'}, | ||
{'marker': {'size': 13}, | ||
'mode': 'markers', | ||
'showlegend': False, | ||
'type': 'scatter', | ||
'x': ['Apple', | ||
'Pear', | ||
'Apple', | ||
'Pear', | ||
'Apple', | ||
'Apple'], | ||
'xaxis': 'x4', | ||
'y': ['Apple', 'Pear', 'Apple', 'Pear', 'Apple', 'Apple'], | ||
'yaxis': 'y4'}], | ||
'layout': {'height': 1000, | ||
'showlegend': True, | ||
'title': 'Scatterplot Matrix', | ||
'width': 1000, | ||
'xaxis1': {'anchor': 'y1', | ||
'domain': [0.0, 0.45]}, | ||
'xaxis2': {'anchor': 'y2', | ||
'domain': [0.55, 1.0]}, | ||
'xaxis3': {'anchor': 'y3', | ||
'domain': [0.0, 0.45], 'title': 'Numbers'}, | ||
'xaxis4': {'anchor': 'y4', | ||
'domain': [0.55, 1.0], 'title': 'Fruit'}, | ||
'yaxis1': {'anchor': 'x1', | ||
'domain': [0.575, 1.0], 'title': 'Numbers'}, | ||
'yaxis2': {'anchor': 'x2', | ||
'domain': [0.575, 1.0]}, | ||
'yaxis3': {'anchor': 'x3', | ||
'domain': [0.0, 0.425], 'title': 'Fruit'}, | ||
'yaxis4': {'anchor': 'x4', | ||
'domain': [0.0, 0.425]}} | ||
} | ||
|
||
self.assert_dict_equal(test_scatter_plot_matrix['data'][0], | ||
exp_scatter_plot_matrix['data'][0]) | ||
|
||
self.assert_dict_equal(test_scatter_plot_matrix['data'][1], | ||
exp_scatter_plot_matrix['data'][1]) | ||
|
||
self.assert_dict_equal(test_scatter_plot_matrix['layout'], | ||
exp_scatter_plot_matrix['layout']) | ||
|
||
def test_scatter_plot_matrix_kwargs(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💯 this is great! thanks! |
||
|
||
# check if test scatter plot matrix matches with | ||
# the expected output | ||
df = pd.DataFrame([[2, 'Apple'], [6, 'Pear'], | ||
[-15, 'Apple'], [5, 'Pear'], | ||
[-2, 'Apple'], [0, 'Apple']], | ||
columns=['Numbers', 'Fruit']) | ||
|
||
test_scatter_plot_matrix = tls.FigureFactory.create_scatterplotmatrix( | ||
df, index='Fruit', endpts=[-10, -1], diag='histogram', | ||
height=1000, width=1000, size=13, title='Scatterplot Matrix', | ||
use_theme=True, palette='YlOrRd', marker=dict(symbol=136) | ||
) | ||
|
||
exp_scatter_plot_matrix = { | ||
'data': [{'marker': {'color': 'rgb(128.0, 0.0, 38.0)'}, | ||
'showlegend': False, | ||
'type': 'histogram', | ||
'x': [2, -15, -2, 0], | ||
'xaxis': 'x1', | ||
'yaxis': 'y1'}, | ||
{'marker': {'color': 'rgb(255.0, 255.0, 204.0)'}, | ||
'showlegend': False, | ||
'type': 'histogram', | ||
'x': [6, 5], | ||
'xaxis': 'x1', | ||
'yaxis': 'y1'}], | ||
'layout': {'barmode': 'stack', | ||
'height': 1000, | ||
'showlegend': True, | ||
'title': 'Scatterplot Matrix', | ||
'width': 1000, | ||
'xaxis1': {'anchor': 'y1', | ||
'domain': [0.0, 1.0], | ||
'title': 'Numbers'}, | ||
'yaxis1': {'anchor': 'x1', | ||
'domain': [0.0, 1.0], | ||
'title': 'Numbers'}} | ||
} | ||
|
||
self.assert_dict_equal(test_scatter_plot_matrix['data'][0], | ||
exp_scatter_plot_matrix['data'][0]) | ||
|
||
self.assert_dict_equal(test_scatter_plot_matrix['data'][1], | ||
exp_scatter_plot_matrix['data'][1]) | ||
|
||
self.assert_dict_equal(test_scatter_plot_matrix['layout'], | ||
exp_scatter_plot_matrix['layout']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/plotly/plotly.py/pull/417/files#r60284049