Skip to content

Commit 90607b5

Browse files
committed
adding subplot titles to make_subplots
Adds titles to subplots and insets. @chriddyp @theengineear @etpinard @aneda @jackparmer
1 parent a526642 commit 90607b5

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

plotly/tools.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,9 @@ def make_subplots(rows=1, cols=1,
630630
Space between subplot rows.
631631
Applies to all rows (use 'specs' subplot-dependents spacing)
632632
633+
subplot_titles (kwarg, list of strings, default=empty list):
634+
Title of each subplot.
635+
633636
specs (kwarg, list of lists of dictionaries):
634637
Subplot specifications.
635638
@@ -719,7 +722,7 @@ def make_subplots(rows=1, cols=1,
719722

720723
# Throw exception if non-valid kwarg is sent
721724
VALID_KWARGS = ['horizontal_spacing', 'vertical_spacing',
722-
'specs', 'insets']
725+
'specs', 'insets', 'subplot_titles']
723726
for key in kwargs.keys():
724727
if key not in VALID_KWARGS:
725728
raise Exception("Invalid keyword argument: '{0}'".format(key))
@@ -734,6 +737,12 @@ def make_subplots(rows=1, cols=1,
734737
except KeyError:
735738
vertical_spacing = 0.3 / rows
736739

740+
# Set 'subplot_titles'
741+
try:
742+
subplot_titles = kwargs['subplot_titles']
743+
except KeyError:
744+
subplot_titles = [""]*rows*cols
745+
737746
# Sanitize 'specs' (must be a list of lists)
738747
exception_msg = "Keyword argument 'specs' must be a list of lists"
739748
try:
@@ -906,6 +915,8 @@ def _get_anchors(r, c, x_cnt, y_cnt, shared_xaxes, shared_yaxes):
906915

907916
return x_anchor, y_anchor
908917

918+
list_of_domains = [] # added for subplot titles
919+
909920
# Function pasting x/y domains in layout object (2d case)
910921
def _add_domain(layout, x_or_y, label, ___domain, anchor, position):
911922
name = label[0] + 'axis' + label[1:]
@@ -916,11 +927,13 @@ def _add_domain(layout, x_or_y, label, ___domain, anchor, position):
916927
if isinstance(position, float):
917928
axis['position'] = position
918929
layout[name] = axis
930+
list_of_domains.append(___domain) # added for subplot titles
919931

920932
# Function pasting x/y domains in layout object (3d case)
921933
def _add_domain_is_3d(layout, s_label, x_domain, y_domain):
922934
scene = graph_objs.Scene(___domain={'x': x_domain, 'y': y_domain})
923935
layout[s_label] = scene
936+
list_of_domains.append(___domain) # added for subplot titles
924937

925938
x_cnt = y_cnt = s_cnt = 1 # subplot axis/scene counters
926939

@@ -1129,6 +1142,34 @@ def _pad(s, cell_len=cell_len):
11291142
s_str + _get_cell_str(r, c, ref) + e_str + '\n'
11301143
)
11311144

1145+
# Add subplot titles
1146+
x_dom = list_of_domains[::2]
1147+
y_dom = list_of_domains[1::2]
1148+
1149+
subtitle_pos_x = []
1150+
for index in range(len(x_dom)):
1151+
subtitle_pos_x.append(((x_dom[index][1])-(x_dom[index][0]))/2 +
1152+
x_dom[index][0])
1153+
1154+
subtitle_pos_y = []
1155+
for index in range(len(y_dom)):
1156+
subtitle_pos_y.append(y_dom[index][1])
1157+
1158+
plot_titles = []
1159+
for index in range(len(x_dom)):
1160+
plot_titles.append({'y': subtitle_pos_y[index],
1161+
'xref': 'paper',
1162+
'x': subtitle_pos_x[index],
1163+
'yref': 'paper',
1164+
'text': subplot_titles[index],
1165+
'showarrow': False,
1166+
'font': Font(size=18),
1167+
'xanchor': 'center',
1168+
'yanchor': 'bottom'
1169+
})
1170+
1171+
layout['annotations'] = plot_titles
1172+
11321173
if print_grid:
11331174
print(grid_str)
11341175

0 commit comments

Comments
 (0)