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 all 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
119 changes: 114 additions & 5 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,13 +2010,13 @@ 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'),
shared_xaxes=True, shared_yaxes=True)
self.assertEqual(fig.to_plotly_json(), expected.to_plotly_json())


def test_subplot_titles_irregular_layout(self):
# make a title for each subplot when the layout is irregular:
expected = Figure(
Expand Down Expand Up @@ -2155,3 +2155,112 @@ 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())

def test_row_width_and_shared_yaxes(self):

expected = Figure({
'data': [],
'layout': {'annotations': [{'font': {'size': 16},
'showarrow': False,
'text': 'Title 1',
'x': 0.225,
'xanchor': 'center',
'xref': 'paper',
'y': 1.0,
'yanchor': 'bottom',
'yref': 'paper'},
{'font': {'size': 16},
'showarrow': False,
'text': 'Title 2',
'x': 0.775,
'xanchor': 'center',
'xref': 'paper',
'y': 1.0,
'yanchor': 'bottom',
'yref': 'paper'},
{'font': {'size': 16},
'showarrow': False,
'text': 'Title 3',
'x': 0.225,
'xanchor': 'center',
'xref': 'paper',
'y': 0.1875,
'yanchor': 'bottom',
'yref': 'paper'},
{'font': {'size': 16},
'showarrow': False,
'text': 'Title 4',
'x': 0.775,
'xanchor': 'center',
'xref': 'paper',
'y': 0.1875,
'yanchor': 'bottom',
'yref': 'paper'}],
'xaxis': {'anchor': 'y', 'domain': [0.0, 0.45]},
'xaxis2': {'anchor': 'free', 'domain': [0.55, 1.0], 'position': 0.4375},
'xaxis3': {'anchor': 'y2', 'domain': [0.0, 0.45]},
'xaxis4': {'anchor': 'free', 'domain': [0.55, 1.0], 'position': 0.0},
'yaxis': {'anchor': 'x', 'domain': [0.4375, 1.0]},
'yaxis2': {'anchor': 'x3', 'domain': [0.0, 0.1875]}}
})

fig = tls.make_subplots(rows=2, cols=2, row_width=[1, 3], shared_yaxes=True,
subplot_titles=('Title 1', 'Title 2', 'Title 3', 'Title 4'))

self.assertEqual(fig.to_plotly_json(), expected.to_plotly_json())

# def test_row_width_and_shared_yaxes(self):
51 changes: 40 additions & 11 deletions plotly/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import six
import copy
import re

from plotly import exceptions, optional_imports, session, utils
from plotly.files import (CONFIG_FILE, CREDENTIALS_FILE, FILE_CONTENT,
Expand Down Expand Up @@ -1001,7 +1002,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 @@ -1323,20 +1323,49 @@ 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_vals = [k for k in layout.to_plotly_json().keys() if 'xaxis' in k]
y_dom_vals = [k for k in layout.to_plotly_json().keys() if 'yaxis' in k]

# sort xaxis and yaxis layout keys
r = re.compile('\d+')

def key_func(m):
try:
return int(r.search(m).group(0))
except AttributeError:
return 0

xaxies_labels_sorted = sorted(x_dom_vals, key=key_func)
yaxies_labels_sorted = sorted(y_dom_vals, key=key_func)

x_dom = [layout[k]['domain'] for k in xaxies_labels_sorted]
y_dom = [layout[k]['domain'] for k in yaxies_labels_sorted]

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