Skip to content

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 104 commits into from
Jan 13, 2015
Merged
Show file tree
Hide file tree
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 Dec 22, 2014
637c5de
sync latest graph ref master
etpinard Dec 29, 2014
55637e0
update get_subplots() docstring
etpinard Dec 30, 2014
1b1d23a
remove old get_subplots() code
etpinard Dec 30, 2014
76c6d4e
add Exception if rows or/and columns are not integers
etpinard Dec 30, 2014
9f1c240
make h/v spacing depend on the number of rows/columns:
etpinard Dec 30, 2014
5406fb3
initialize specs list of lists:
etpinard Dec 30, 2014
26f704e
compute some common distances
etpinard Dec 30, 2014
d9341c8
loop through specs:
etpinard Dec 30, 2014
8705058
add _get_shared()
etpinard Dec 30, 2014
842ff88
add _add_domain() and _add_domain_is3D()
etpinard Dec 30, 2014
c55a92f
add _fill_grid()
etpinard Dec 30, 2014
34ee502
initialize looping variables
etpinard Dec 30, 2014
97145a5
(oops) get_shared -> _get_shared
etpinard Dec 30, 2014
5dfcfad
add print_grid
etpinard Dec 30, 2014
f72a249
modify computed domain in existing tests (because of rounding errors)
etpinard Dec 30, 2014
857a7d7
modify arguments in existing tests:
etpinard Dec 30, 2014
e64794b
test galore git diff --cached !
etpinard Dec 30, 2014
ab661f4
improve algorithm (part 1):
etpinard Dec 31, 2014
eced9a3
improve algorithm (part 2):
etpinard Dec 31, 2014
55a9b3f
improve algorithm (part 3):
etpinard Dec 31, 2014
3eba0d7
improve algorithm (part 4):
etpinard Dec 31, 2014
79c9868
improve algorthm (part 5):
etpinard Dec 31, 2014
ca08227
remove _get_shared()
etpinard Dec 31, 2014
4efbb4f
add _get_label()
etpinard Dec 31, 2014
69214c6
add _get_anchors
etpinard Dec 31, 2014
0497009
update _add_domain and _add_domain_is3D
etpinard Dec 31, 2014
a83ad48
code aesthetics
etpinard Dec 31, 2014
2d781f6
modify expected values in tests due to rounding errors
etpinard Dec 31, 2014
b8e9f53
add 2nd rowspan test (that discovered the bug)
etpinard Dec 31, 2014
46ed9d7
more test updating due to rounding errors
etpinard Dec 31, 2014
540b734
update shared axis tests
etpinard Dec 31, 2014
7b0e8fb
add 1 shared_xaxes=True, shared_yaxes=True test
etpinard Dec 31, 2014
f6a64d1
sub isEmpty -> is_empyt , is3D -> is_3d
etpinard Jan 2, 2015
bed60db
add info about 'insets' in docstring
etpinard Jan 4, 2015
50eeb8f
add try/except block for kwargs['insets']
etpinard Jan 4, 2015
1a2192e
aesthestic in SPEC_defaults
etpinard Jan 4, 2015
443217e
add functions checking and filling keys (for 'specs' & 'insets')
etpinard Jan 4, 2015
a72f043
send specs to _check_keys_and_fill()
etpinard Jan 4, 2015
8e3e83d
check and fill 'insets'
etpinard Jan 4, 2015
d30e7b9
raise Exception if non-valid kwarg is sent to get_subplots
etpinard Jan 4, 2015
f9235b3
loop through insets!
etpinard Jan 4, 2015
dfa47f7
add inset str reprensation
etpinard Jan 4, 2015
3ef8116
add wrong key & wrong type tests
etpinard Jan 4, 2015
ec25258
add 2 insets tests
etpinard Jan 4, 2015
25c8d85
update 'specs' docstring
etpinard Jan 4, 2015
330eeac
raise Exception if specs dims not equal to 'rows' and 'columns'
etpinard Jan 4, 2015
12cdffd
rm 'is_empty' from SPEC_defaults
etpinard Jan 4, 2015
e52ff4f
add cond for spec == None case in check_and_fill
etpinard Jan 4, 2015
bad84cd
pep8 tuple
etpinard Jan 4, 2015
0c83b9d
move spec is None cond at top of spec loop
etpinard Jan 4, 2015
45229d2
re-align specs loop
etpinard Jan 5, 2015
fa286a0
move right by one column:
etpinard Jan 5, 2015
f0c3997
add under/over-specified specs tests
etpinard Jan 5, 2015
feea0cc
update tests:
etpinard Jan 5, 2015
98b955f
add a colspan>1 + rowspan>1 in the same cell test
etpinard Jan 5, 2015
e5d753f
[need some feedback] add start_cell docstring
etpinard Jan 5, 2015
0d74fb3
add back the old get_subplots :
etpinard Jan 6, 2015
6df366e
mv get_subplots argument checks tests to the top of file
etpinard Jan 6, 2015
5bcbaaa
move make_subplots tests to test_make_subplots.py
etpinard Jan 6, 2015
2edb810
pep8 get_subplots docstring
etpinard Jan 6, 2015
421dd5f
improve make_subplots docstring
etpinard Jan 7, 2015
af67e90
replace 'columns' with 'cols'
etpinard Jan 7, 2015
7bb6ba2
rm '>>>' in docstring
etpinard Jan 7, 2015
67672dd
force 'specs' to be list of lists
etpinard Jan 7, 2015
a311719
force items in 'specs' to be either dicts or None
etpinard Jan 7, 2015
dd16fa2
define START_CELL and build grid using it
etpinard Jan 7, 2015
254cf13
sub 'grid_str' -> 'grid_ref':
etpinard Jan 7, 2015
46b6c73
fill in a layout obj, not a figure obj directly
etpinard Jan 7, 2015
4a6b718
remove i, j indices
etpinard Jan 7, 2015
1cb0026
improve and generalize algorithm:
etpinard Jan 7, 2015
964daff
rm grid_str code (taken care outside specs loop now)
etpinard Jan 7, 2015
7463f60
update (r, c) convention:
etpinard Jan 7, 2015
f0eea15
update _add_domain (accepts layout not fig)
etpinard Jan 7, 2015
95cded8
update insets loop:
etpinard Jan 7, 2015
7b0e5d1
improve 'grid_str' code
etpinard Jan 7, 2015
3f7572a
pep8 and add TODO
etpinard Jan 7, 2015
e9c2e01
test galore !!!
etpinard Jan 7, 2015
bfa69ce
add exception for out of range 'insets.cell'
etpinard Jan 7, 2015
0f75a2d
add tests for prev commit
etpinard Jan 7, 2015
9c34d84
sync for graph ref (trace 'scene')
etpinard Jan 7, 2015
d39a0be
make print_grid=True the default for make_subplots
etpinard Jan 9, 2015
87d645a
sub '!' -> ':' in print grid msg
etpinard Jan 9, 2015
08892a9
update shared_{x,y}axes docstring
etpinard Jan 9, 2015
e4d4936
capitalize ROW_DIR
etpinard Jan 9, 2015
2de9ad7
update get_anchors logic:
etpinard Jan 9, 2015
bd16672
add shared axes tests
etpinard Jan 9, 2015
6b2465b
paste grid_ref and grid_str to fig in make_subplots
etpinard Jan 9, 2015
0437513
add print_grid method to Figure
etpinard Jan 9, 2015
9a2767b
add append_trace method to Figure
etpinard Jan 9, 2015
f13a408
make _grid_ref and _grid_str attributes
etpinard Jan 9, 2015
857013c
fix typo in exception msg
etpinard Jan 9, 2015
7e2d3e0
better test name
etpinard Jan 9, 2015
ff0a112
add exception for row/col =< 0
etpinard Jan 9, 2015
8dc3f80
add tests for append_trace
etpinard Jan 9, 2015
87501d8
force set axis.position when anchor == 'free'
etpinard Jan 9, 2015
cebfae2
update tests
etpinard Jan 9, 2015
68aee36
update make_subplot docstring
etpinard Jan 9, 2015
4ee9b59
add docstring to append_trace and print_grid
etpinard Jan 9, 2015
d2d7a92
add Exception if rows / cols <= 0
etpinard Jan 10, 2015
1f1960d
add tests about last commit
etpinard Jan 10, 2015
e0301ad
add example in Figure.append_trace docstring on ref in make_subplots
etpinard Jan 10, 2015
3ac711f
v 1.5.0
etpinard Jan 10, 2015
5536194
Merge branch 'master' into revamp-get_subplots
etpinard Jan 10, 2015
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
81 changes: 81 additions & 0 deletions plotly/graph_objs/graph_objs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

Choose a reason for hiding this comment

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

docstring!

Print a visual layout of the figure's axes arrangement. This is only valid for figures that are created with plotly.tools.make_subplots.

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:
Copy link
Member

Choose a reason for hiding this comment

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

docstring!

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.

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)
Expand Down
18 changes: 14 additions & 4 deletions plotly/graph_reference/graph_objs_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -1586,9 +1586,9 @@
},
"scene": {
"key_type": "plot_info",
"val_types": "'s1' | 's2' | 's3' | etc.",
"val_types": "'scene1' | 'scene2' | 'scene3' | etc.",
"required": false,
"description": "This key determines the scene on which this trace will be plotted in. More info coming soon."
"description": "This key determines the scene on which this trace will be plotted in."
},
"stream": {
"key_type": "object",
Expand Down Expand Up @@ -1657,9 +1657,9 @@
},
"scene": {
"key_type": "plot_info",
"val_types": "'s1' | 's2' | 's3' | etc.",
"val_types": "'scene1' | 'scene2' | 'scene3' | etc.",
"required": false,
"description": "This key determines the scene on which this trace will be plotted in. More info coming soon."
"description": "This key determines the scene on which this trace will be plotted in."
},
"stream": {
"key_type": "object",
Expand Down Expand Up @@ -3317,6 +3317,16 @@
],
"description": "Sets the camera position with respect to the scene. The first entry (a list or 1d numpy array of length 4) sets the angular position of the camera. The second entry (a list or 1d numpy array of length 3) sets the (x,y,z) translation of the camera. The third entry (a scalar) sets zoom of the camera."
},
"domain": {
"key_type": "plot_info",
"val_types": "domain dictionary",
"required": false,
"examples": [
"{'x': [0, 0.4], 'y': [0.6, 1]}",
"dict(x=[0, 0.4], y=[0.6, 1])"
],
"description": "Sets the x-y domain of this scene on the plotting surface."
},
"bgcolor": {
"key_type": "style",
"val_types": "a string describing color",
Expand Down
176 changes: 176 additions & 0 deletions plotly/tests/test_core/test_graph_objs/test_append_trace.py
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)
Loading