-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Revamp tls.get_subplots #170
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
Changes from all commits
Commits
Show all changes
104 commits
Select commit
Hold shift + click to select a range
9b2f4c0
add specs to revamped get_subplots()
etpinard 637c5de
sync latest graph ref master
etpinard 55637e0
update get_subplots() docstring
etpinard 1b1d23a
remove old get_subplots() code
etpinard 76c6d4e
add Exception if rows or/and columns are not integers
etpinard 9f1c240
make h/v spacing depend on the number of rows/columns:
etpinard 5406fb3
initialize specs list of lists:
etpinard 26f704e
compute some common distances
etpinard d9341c8
loop through specs:
etpinard 8705058
add _get_shared()
etpinard 842ff88
add _add_domain() and _add_domain_is3D()
etpinard c55a92f
add _fill_grid()
etpinard 34ee502
initialize looping variables
etpinard 97145a5
(oops) get_shared -> _get_shared
etpinard 5dfcfad
add print_grid
etpinard f72a249
modify computed domain in existing tests (because of rounding errors)
etpinard 857a7d7
modify arguments in existing tests:
etpinard e64794b
test galore git diff --cached !
etpinard ab661f4
improve algorithm (part 1):
etpinard eced9a3
improve algorithm (part 2):
etpinard 55a9b3f
improve algorithm (part 3):
etpinard 3eba0d7
improve algorithm (part 4):
etpinard 79c9868
improve algorthm (part 5):
etpinard ca08227
remove _get_shared()
etpinard 4efbb4f
add _get_label()
etpinard 69214c6
add _get_anchors
etpinard 0497009
update _add_domain and _add_domain_is3D
etpinard a83ad48
code aesthetics
etpinard 2d781f6
modify expected values in tests due to rounding errors
etpinard b8e9f53
add 2nd rowspan test (that discovered the bug)
etpinard 46ed9d7
more test updating due to rounding errors
etpinard 540b734
update shared axis tests
etpinard 7b0e8fb
add 1 shared_xaxes=True, shared_yaxes=True test
etpinard f6a64d1
sub isEmpty -> is_empyt , is3D -> is_3d
etpinard bed60db
add info about 'insets' in docstring
etpinard 50eeb8f
add try/except block for kwargs['insets']
etpinard 1a2192e
aesthestic in SPEC_defaults
etpinard 443217e
add functions checking and filling keys (for 'specs' & 'insets')
etpinard a72f043
send specs to _check_keys_and_fill()
etpinard 8e3e83d
check and fill 'insets'
etpinard d30e7b9
raise Exception if non-valid kwarg is sent to get_subplots
etpinard f9235b3
loop through insets!
etpinard dfa47f7
add inset str reprensation
etpinard 3ef8116
add wrong key & wrong type tests
etpinard ec25258
add 2 insets tests
etpinard 25c8d85
update 'specs' docstring
etpinard 330eeac
raise Exception if specs dims not equal to 'rows' and 'columns'
etpinard 12cdffd
rm 'is_empty' from SPEC_defaults
etpinard e52ff4f
add cond for spec == None case in check_and_fill
etpinard bad84cd
pep8 tuple
etpinard 0c83b9d
move spec is None cond at top of spec loop
etpinard 45229d2
re-align specs loop
etpinard fa286a0
move right by one column:
etpinard f0c3997
add under/over-specified specs tests
etpinard feea0cc
update tests:
etpinard 98b955f
add a colspan>1 + rowspan>1 in the same cell test
etpinard e5d753f
[need some feedback] add start_cell docstring
etpinard 0d74fb3
add back the old get_subplots :
etpinard 6df366e
mv get_subplots argument checks tests to the top of file
etpinard 5bcbaaa
move make_subplots tests to test_make_subplots.py
etpinard 2edb810
pep8 get_subplots docstring
etpinard 421dd5f
improve make_subplots docstring
etpinard af67e90
replace 'columns' with 'cols'
etpinard 7bb6ba2
rm '>>>' in docstring
etpinard 67672dd
force 'specs' to be list of lists
etpinard a311719
force items in 'specs' to be either dicts or None
etpinard dd16fa2
define START_CELL and build grid using it
etpinard 254cf13
sub 'grid_str' -> 'grid_ref':
etpinard 46b6c73
fill in a layout obj, not a figure obj directly
etpinard 4a6b718
remove i, j indices
etpinard 1cb0026
improve and generalize algorithm:
etpinard 964daff
rm grid_str code (taken care outside specs loop now)
etpinard 7463f60
update (r, c) convention:
etpinard f0eea15
update _add_domain (accepts layout not fig)
etpinard 95cded8
update insets loop:
etpinard 7b0e5d1
improve 'grid_str' code
etpinard 3f7572a
pep8 and add TODO
etpinard e9c2e01
test galore !!!
etpinard bfa69ce
add exception for out of range 'insets.cell'
etpinard 0f75a2d
add tests for prev commit
etpinard 9c34d84
sync for graph ref (trace 'scene')
etpinard d39a0be
make print_grid=True the default for make_subplots
etpinard 87d645a
sub '!' -> ':' in print grid msg
etpinard 08892a9
update shared_{x,y}axes docstring
etpinard e4d4936
capitalize ROW_DIR
etpinard 2de9ad7
update get_anchors logic:
etpinard bd16672
add shared axes tests
etpinard 6b2465b
paste grid_ref and grid_str to fig in make_subplots
etpinard 0437513
add print_grid method to Figure
etpinard 9a2767b
add append_trace method to Figure
etpinard f13a408
make _grid_ref and _grid_str attributes
etpinard 857013c
fix typo in exception msg
etpinard 7e2d3e0
better test name
etpinard ff0a112
add exception for row/col =< 0
etpinard 8dc3f80
add tests for append_trace
etpinard 87501d8
force set axis.position when anchor == 'free'
etpinard cebfae2
update tests
etpinard 68aee36
update make_subplot docstring
etpinard 4ee9b59
add docstring to append_trace and print_grid
etpinard d2d7a92
add Exception if rows / cols <= 0
etpinard 1f1960d
add tests about last commit
etpinard e0301ad
add example in Figure.append_trace docstring on ref in make_subplots
etpinard 3ac711f
v 1.5.0
etpinard 5536194
Merge branch 'master' into revamp-get_subplots
etpinard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -919,6 +919,87 @@ def __init__(self, *args, **kwargs): | |
kwargs['layout'] = Layout() | ||
super(Figure, self).__init__(*args, **kwargs) | ||
Figure.__init__ = __init__ # override method! | ||
|
||
def print_grid(self): | ||
"""Print a visual layout of the figure's axes arrangement. | ||
|
||
This is only valid for figures that are created | ||
with plotly.tools.make_subplots. | ||
""" | ||
try: | ||
grid_str = self._grid_str | ||
except KeyError: | ||
raise Exception("Use plotly.tools.make_subplots " | ||
"to create a subplot grid.") | ||
print(grid_str) | ||
Figure.print_grid = print_grid | ||
|
||
def append_trace(self, trace, row, col): | ||
""" Helper function to add a data traces to your figure | ||
that is bound to axes at the row, col index. | ||
|
||
The row, col index is generated from figures created with | ||
plotly.tools.make_subplots and can be viewed with Figure.print_grid. | ||
|
||
Example: | ||
# stack two subplots vertically | ||
fig = tools.make_subplots(rows=2) | ||
|
||
This is the format of your plot grid: | ||
[ (1,1) x1,y1 ] | ||
[ (2,1) x2,y2 ] | ||
|
||
fig.append_trace(Scatter(x=[1,2,3], y=[2,1,2]), 1, 1) | ||
fig.append_trace(Scatter(x=[1,2,3], y=[2,1,2]), 2, 1) | ||
|
||
Arguments: | ||
|
||
trace (plotly trace object): | ||
The data trace to be bound. | ||
|
||
row (int): | ||
Subplot row index on the subplot grid (see Figure.print_grid) | ||
|
||
col (int): | ||
Subplot column index on the subplot grid (see Figure.print_grid) | ||
|
||
""" | ||
try: | ||
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. docstring!
|
||
grid_ref = self._grid_ref | ||
except KeyError: | ||
raise Exception("In order to use Figure.append_trace, " | ||
"you must first use plotly.tools.make_subplots " | ||
"to create a subplot grid.") | ||
if row <= 0: | ||
raise Exception("Row value is out of range. " | ||
"Note: the starting cell is (1, 1)") | ||
if col <= 0: | ||
raise Exception("Col value is out of range. " | ||
"Note: the starting cell is (1, 1)") | ||
try: | ||
ref = grid_ref[row-1][col-1] | ||
except IndexError: | ||
raise Exception("The (row, col) pair sent is out of range. " | ||
"Use Figure.print_grid to view the subplot grid. ") | ||
if 'scene' in ref[0]: | ||
trace['scene'] = ref[0] | ||
if ref[0] not in self['layout']: | ||
raise Exception("Something went wrong. " | ||
"The scene object for ({r},{c}) subplot cell " | ||
"got deleted.".format(r=row, c=col)) | ||
else: | ||
xaxis_key = "xaxis{ref}".format(ref=ref[0][1:]) | ||
yaxis_key = "yaxis{ref}".format(ref=ref[1][1:]) | ||
if (xaxis_key not in self['layout'] | ||
or yaxis_key not in self['layout']): | ||
raise Exception("Something went wrong. " | ||
"An axis object for ({r},{c}) subplot cell " | ||
"got deleted.".format(r=row, c=col)) | ||
trace['xaxis'] = ref[0] | ||
trace['yaxis'] = ref[1] | ||
self['data'] += [trace] | ||
Figure.append_trace = append_trace | ||
|
||
return Figure | ||
|
||
Figure = get_patched_figure_class(Figure) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
plotly/tests/test_core/test_graph_objs/test_append_trace.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
import plotly | ||
from plotly.graph_objs import * | ||
import plotly.tools as tls | ||
from nose.tools import raises | ||
|
||
|
||
@raises(Exception) | ||
def test_print_grid_before_make_subplots(): | ||
fig.print_grid() | ||
|
||
@raises(Exception) | ||
def test_append_trace_before_make_subplots(): | ||
trace = Scatter( | ||
x=[1,2,3], | ||
y=[2,3,4] | ||
) | ||
fig.append_trace(trace, 2, 2) | ||
|
||
@raises(Exception) | ||
def test_append_trace_row_out_of_range(): | ||
trace = Scatter( | ||
x=[1,2,3], | ||
y=[2,3,4] | ||
) | ||
fig = tls.make_subplots(rows=2, cols=3) | ||
fig.append_trace(trace, 10, 2) | ||
|
||
@raises(Exception) | ||
def test_append_trace_col_out_of_range(): | ||
trace = Scatter( | ||
x=[1,2,3], | ||
y=[2,3,4] | ||
) | ||
fig = tls.make_subplots(rows=2, cols=3) | ||
fig.append_trace(trace, 2, 0) | ||
|
||
def test_append_scatter(): | ||
expected = Figure( | ||
data=Data([ | ||
Scatter( | ||
x=[1, 2, 3], | ||
y=[2, 3, 4], | ||
xaxis='x5', | ||
yaxis='y5' | ||
) | ||
]), | ||
layout=Layout( | ||
xaxis1=XAxis( | ||
domain=[0.0, 0.2888888888888889], | ||
anchor='y1' | ||
), | ||
xaxis2=XAxis( | ||
domain=[0.35555555555555557, 0.6444444444444445], | ||
anchor='y2' | ||
), | ||
xaxis3=XAxis( | ||
domain=[0.7111111111111111, 1.0], | ||
anchor='y3' | ||
), | ||
xaxis4=XAxis( | ||
domain=[0.0, 0.2888888888888889], | ||
anchor='y4' | ||
), | ||
xaxis5=XAxis( | ||
domain=[0.35555555555555557, 0.6444444444444445], | ||
anchor='y5' | ||
), | ||
xaxis6=XAxis( | ||
domain=[0.7111111111111111, 1.0], | ||
anchor='y6' | ||
), | ||
yaxis1=YAxis( | ||
domain=[0.575, 1.0], | ||
anchor='x1' | ||
), | ||
yaxis2=YAxis( | ||
domain=[0.575, 1.0], | ||
anchor='x2' | ||
), | ||
yaxis3=YAxis( | ||
domain=[0.575, 1.0], | ||
anchor='x3' | ||
), | ||
yaxis4=YAxis( | ||
domain=[0.0, 0.425], | ||
anchor='x4' | ||
), | ||
yaxis5=YAxis( | ||
domain=[0.0, 0.425], | ||
anchor='x5' | ||
), | ||
yaxis6=YAxis( | ||
domain=[0.0, 0.425], | ||
anchor='x6' | ||
) | ||
) | ||
) | ||
|
||
trace = Scatter( | ||
x=[1,2,3], | ||
y=[2,3,4] | ||
) | ||
fig = tls.make_subplots(rows=2, cols=3) | ||
fig.append_trace(trace, 2, 2) | ||
assert fig == expected | ||
|
||
@raises(Exception) | ||
def test_append_scatter_after_deleting_xaxis(): | ||
trace = Scatter( | ||
x=[1,2,3], | ||
y=[2,3,4] | ||
) | ||
fig = tls.make_subplots(rows=2, cols=3) | ||
fig['layout'].pop('xaxis5', None) | ||
fig.append_trace(trace, 2, 2) | ||
|
||
@raises(Exception) | ||
def test_append_scatter_after_deleting_yaxis(): | ||
trace = Scatter( | ||
x=[1,2,3], | ||
y=[2,3,4] | ||
) | ||
fig = tls.make_subplots(rows=2, cols=3) | ||
fig['layout'].pop('yaxis5', None) | ||
fig.append_trace(trace, 2, 2) | ||
|
||
def test_append_scatter3d(): | ||
expected = Figure( | ||
data=Data([ | ||
Scatter3d( | ||
x=[1, 2, 3], | ||
y=[2, 3, 4], | ||
z=[1, 2, 3], | ||
scene='scene2' | ||
), | ||
Scatter3d( | ||
x=[1, 2, 3], | ||
y=[2, 3, 4], | ||
z=[1, 2, 3], | ||
scene='scene2' | ||
) | ||
]), | ||
layout=Layout( | ||
scene1=Scene( | ||
domain={'y': [0.575, 1.0], 'x': [0.0, 1.0]} | ||
), | ||
scene2=Scene( | ||
domain={'y': [0.0, 0.425], 'x': [0.0, 1.0]} | ||
) | ||
) | ||
) | ||
|
||
fig = tls.make_subplots(rows=2, cols=1, | ||
specs=[[{'is_3d': True}], | ||
[{'is_3d': True}]]) | ||
trace = Scatter3d( | ||
x=[1,2,3], | ||
y=[2,3,4], | ||
z=[1,2,3] | ||
) | ||
fig.append_trace(trace, 1, 1) | ||
fig.append_trace(trace, 2, 1) | ||
assert fig == expected | ||
|
||
@raises(Exception) | ||
def test_append_scatter3d_after_deleting_scene(): | ||
fig = tls.make_subplots(rows=2, cols=1, | ||
specs=[[{'is_3d': True}], | ||
[{'is_3d': True}]]) | ||
trace = Scatter3d( | ||
x=[1,2,3], | ||
y=[2,3,4], | ||
z=[1,2,3] | ||
) | ||
fig['layout'].pop('scene1', None) | ||
fig.append_trace(trace, 1, 1) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
docstring!