Skip to content

Commit 1cb0026

Browse files
committed
improve and generalize algorithm:
- loop through specs with r, c indices - compute c_spanned and r_spanned up front - throw exception if c_spanned or r_spanned is too long for grid - add cond around row_dir (for 'start_cell='top-left' | 'bottom-left') - pass layout (not fig) to _add_domain - fill in grid_ref !
1 parent 4a6b718 commit 1cb0026

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

plotly/tools.py

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -882,59 +882,71 @@ def _add_domain_is_3d(fig, s_cnt, x_domain, y_domain):
882882

883883
x_cnt = y_cnt = s_cnt = 1 # subplot axis/scene counters
884884

885-
# Loop through specs
886-
for row, spec_row in enumerate(specs):
885+
# Loop through specs -- (r, c) <-> (row, col)
886+
for r, spec_row in enumerate(specs):
887+
for c, spec in enumerate(spec_row):
887888

889+
if spec is None: # skip over None cells
890+
continue
888891

889-
for col, spec in enumerate(spec_row):
892+
c_spanned = c + spec['colspan'] - 1 # get spanned c
893+
r_spanned = r + spec['rowspan'] - 1 # get spanned r
890894

891-
# String representation for empty cells
892-
if spec is None:
893-
if print_grid and grid_str[i][j] == '':
894-
grid_str[i][j] = '{none}'
895-
j += 1
896-
continue
895+
# Throw exception if 'colspan' | 'rowspan' is too large for grid
896+
if c_spanned >= cols:
897+
raise Exception("Some 'colspan' value is too large for "
898+
"this subplot grid.")
899+
if r_spanned >= rows:
900+
raise Exception("Some 'rowspan' value is too large for "
901+
"this subplot grid.")
897902

898903
# Get x domain using grid and colspan
899-
x_s = grid[i][j][0] + spec['l']
900-
x_e = grid[i][j+(spec['colspan']-1)][0] + width - spec['r']
904+
x_s = grid[r][c][0] + spec['l']
905+
x_e = grid[r][c_spanned][0] + width - spec['r']
901906
x_domain = [x_s, x_e]
902907

903-
# Get y domain using grid and rowspan
904-
y_s = grid[i][j][1] + spec['b']
905-
y_e = grid[i+(spec['rowspan']-1)][j][1] + height - spec['t']
908+
# Get y domain (dep. on row_dir) using grid & r_spanned
909+
if row_dir > 0:
910+
y_s = grid[r][c][1] + spec['b']
911+
y_e = grid[r_spanned][c][1] + height - spec['t']
912+
else:
913+
y_s = grid[r_spanned][c][1] + spec['b']
914+
y_e = grid[r][c][1] + height - spec['t']
906915
y_domain = [y_s, y_e]
907916

908917
if spec['is_3d']:
918+
909919
# Add scene to layout
910-
_add_domain_is_3d(fig, s_cnt, x_domain, y_domain)
911-
if print_grid:
912-
grid_str[i][j] = '[scene{}'.format(s_cnt)
920+
s_label = 'scene{0}'.format(s_cnt)
921+
_add_domain_is_3d(layout, s_label, x_domain, y_domain)
922+
grid_ref[r][c] = (s_label, )
913923
s_cnt += 1
924+
914925
else:
915926

916927
# Get axis label and anchor
917-
x_label = _get_label('x', row, col, x_cnt, shared_xaxes)
918-
y_label = _get_label('y', row, col, y_cnt, shared_yaxes)
919-
x_anchor, y_anchor = _get_anchors(row, col,
928+
x_label = _get_label('x', r, c, x_cnt, shared_xaxes)
929+
y_label = _get_label('y', r, c, y_cnt, shared_yaxes)
930+
x_anchor, y_anchor = _get_anchors(r, c,
920931
x_cnt, y_cnt,
921932
shared_xaxes,
922933
shared_yaxes)
923934

924935
# Add a xaxis to layout (N.B anchor == False -> no axis)
925936
if x_anchor:
926937
x_position = y_domain[0] if x_anchor == 'free' else 0
927-
_add_domain(fig, 'x', x_label, x_domain,
938+
_add_domain(layout, 'x', x_label, x_domain,
928939
x_anchor, x_position)
929940
x_cnt += 1
930941

931942
# Add a yaxis to layout (N.B anchor == False -> no axis)
932943
if y_anchor:
933944
y_position = x_domain[0] if y_anchor == 'free' else 0
934-
_add_domain(fig, 'y', y_label, y_domain,
945+
_add_domain(layout, 'y', y_label, y_domain,
935946
y_anchor, y_position)
936947
y_cnt += 1
937948

949+
grid_ref[r][c] = (x_label, y_label) # fill in ref
938950
if print_grid:
939951
grid_str[i][j] = '[{},{}'.format(x_label, y_label)
940952

0 commit comments

Comments
 (0)