Skip to content

subplot titles respecting row_width and column_width #1245

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 5 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
63 changes: 59 additions & 4 deletions plotly/tests/test_core/test_tools/test_make_subplots.py
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,7 @@ def test_subplot_titles_shared_axes(self):
layout=Layout(
annotations=Annotations([
Annotation(
x=0.22499999999999998,
x=0.225,
y=1.0,
xref='paper',
yref='paper',
Expand All @@ -1957,7 +1957,7 @@ def test_subplot_titles_shared_axes(self):
yanchor='bottom'
),
Annotation(
x=0.7749999999999999,
x=0.775,
y=1.0,
xref='paper',
yref='paper',
Expand All @@ -1968,7 +1968,7 @@ def test_subplot_titles_shared_axes(self):
yanchor='bottom'
),
Annotation(
x=0.22499999999999998,
x=0.225,
y=0.375,
xref='paper',
yref='paper',
Expand All @@ -1979,7 +1979,7 @@ def test_subplot_titles_shared_axes(self):
yanchor='bottom'
),
Annotation(
x=0.7749999999999999,
x=0.775,
y=0.375,
xref='paper',
yref='paper',
Expand Down Expand Up @@ -2010,6 +2010,7 @@ def test_subplot_titles_shared_axes(self):
)
)
)

fig = tls.make_subplots(rows=2, cols=2,
subplot_titles=('Title 1', 'Title 2',
'Title 3', 'Title 4'),
Expand Down Expand Up @@ -2155,3 +2156,57 @@ def test_large_columns_no_errors(self):
fig = tls.make_subplots(100, 1,
vertical_spacing=v_space,
specs=[[{'is_3d': True}] for _ in range(100)])

def test_row_width_and_column_width(self):

expected = Figure({
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add an image of this expected figure to the PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just a png in the file? I am not aware of image tests for this repo but I could be wrong

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I just meant in the GitHub PR so I could see what it looks like 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

newplot

'data': [],
'layout': {'annotations': [{'font': {'size': 16},
'showarrow': False,
'text': 'Title 1',
'x': 0.405,
'xanchor': 'center',
'xref': 'paper',
'y': 1.0,
'yanchor': 'bottom',
'yref': 'paper'},
{'font': {'size': 16},
'showarrow': False,
'text': 'Title 2',
'x': 0.9550000000000001,
'xanchor': 'center',
'xref': 'paper',
'y': 1.0,
'yanchor': 'bottom',
'yref': 'paper'},
{'font': {'size': 16},
'showarrow': False,
'text': 'Title 3',
'x': 0.405,
'xanchor': 'center',
'xref': 'paper',
'y': 0.1875,
'yanchor': 'bottom',
'yref': 'paper'},
{'font': {'size': 16},
'showarrow': False,
'text': 'Title 4',
'x': 0.9550000000000001,
'xanchor': 'center',
'xref': 'paper',
'y': 0.1875,
'yanchor': 'bottom',
'yref': 'paper'}],
'xaxis': {'anchor': 'y', 'domain': [0.0, 0.81]},
'xaxis2': {'anchor': 'y2', 'domain': [0.91, 1.0]},
'xaxis3': {'anchor': 'y3', 'domain': [0.0, 0.81]},
'xaxis4': {'anchor': 'y4', 'domain': [0.91, 1.0]},
'yaxis': {'anchor': 'x', 'domain': [0.4375, 1.0]},
'yaxis2': {'anchor': 'x2', 'domain': [0.4375, 1.0]},
'yaxis3': {'anchor': 'x3', 'domain': [0.0, 0.1875]},
'yaxis4': {'anchor': 'x4', 'domain': [0.0, 0.1875]}}
})
fig = tls.make_subplots(rows=2, cols=2,
subplot_titles=('Title 1', 'Title 2', 'Title 3', 'Title 4'),
row_width=[1, 3], column_width=[9, 1])
self.assertEqual(fig.to_plotly_json(), expected.to_plotly_json())
35 changes: 24 additions & 11 deletions plotly/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,6 @@ def _checks(item, defaults):
) for c in col_seq
] for r in row_seq
]

# [grid_ref] Initialize the grid and insets' axis-reference lists
grid_ref = [[None for c in range(cols)] for r in range(rows)]
insets_ref = [None for inset in range(len(insets))] if insets else None
Expand Down Expand Up @@ -1080,6 +1079,7 @@ def _get_anchors(r, c, x_cnt, y_cnt, shared_xaxes, shared_yaxes):
return x_anchor, y_anchor

list_of_domains = [] # added for subplot titles
list_of_domains_complete = [] # hold onto every domain used, even if shared axes
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this used anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope - I'll ⚡️


# Function pasting x/y domains in layout object (2d case)
def _add_domain(layout, x_or_y, label, domain, anchor, position):
Expand Down Expand Up @@ -1323,20 +1323,33 @@ def _pad(s, cell_len=cell_len):
subtitle_pos_x.append(sum(x_domains) / 2)
for y_domains in y_dom:
subtitle_pos_y.append(y_domains[1])

# If shared_axes is True the domin of each subplot is not returned so the
# title position must be calculated for each subplot
else:
subtitle_pos_x = [None] * cols
subtitle_pos_y = [None] * rows
delt_x = (x_e - x_s)
x_dom = [layout[k]['domain'] for k in sorted(layout.to_plotly_json().keys()) if 'xaxis' in k]
y_dom = [layout[k]['domain'] for k in sorted(layout.to_plotly_json().keys()) if 'yaxis' in k]
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this sort going to break down when there are more than 9 xaxes? e.g. ['xaxis', 'xaxis10', 'xaxis2', 'xaxis3', ...]
I'm thinking we might need to pluck off the digits and sort them as integers. Could you try an example with 11 or 12 rows and see if there's an issue? If there is an issue, let's make it a test 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great catch - I'll make sure it's full proof

for index in range(cols):
subtitle_pos_x[index] = ((delt_x / 2) +
((delt_x + horizontal_spacing) * index))
subtitle_pos_x *= rows
for index in range(rows):
subtitle_pos_y[index] = (1 - ((y_e + vertical_spacing) * index))
subtitle_pos_y *= cols
subtitle_pos_y = sorted(subtitle_pos_y, reverse=True)
subtitle_pos_x = []
for x_domains in x_dom:
subtitle_pos_x.append(sum(x_domains) / 2)
subtitle_pos_x *= rows

if shared_yaxes:
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we have any tests that specify row_width and hit this branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no, i'll write one

for index in range(rows):
subtitle_pos_y = []
for y_domain in y_dom:
subtitle_pos_y.append(y_domain[1])
subtitle_pos_y *= cols
subtitle_pos_y = sorted(subtitle_pos_y, reverse=True)

else:
for index in range(rows):
subtitle_pos_y = []
for y_domain in y_dom:
subtitle_pos_y.append(y_domain[1])
subtitle_pos_y = sorted(subtitle_pos_y, reverse=True)
subtitle_pos_y *= cols

plot_titles = []
for index in range(len(subplot_titles)):
Expand Down