В дополнение к предыдущим ответам, если кто-то хочет автоматизировать операцию, я написал функцию, которая облегчит эту задачу.вот оно:
def split_title_line(title_text, split_on='(', max_words=5): # , max_words=None):
"""
A function that splits any string based on specific character
(returning it with the string), with maximum number of words on it
"""
split_at = title_text.find (split_on)
ti = title_text
if split_at > 1:
ti = ti.split (split_on)
for i, tx in enumerate (ti[1:]):
ti[i + 1] = split_on + tx
if type (ti) == type ('text'):
ti = [ti]
for j, td in enumerate (ti):
if td.find (split_on) > 0:
pass
else:
tw = td.split ()
t2 = []
for i in range (0, len (tw), max_words):
t2.append (' '.join (tw[i:max_words + i]))
ti[j] = t2
ti = [item for sublist in ti for item in sublist]
ret_tex = []
for j in range (len (ti)):
for i in range(0, len(ti)-1, 2):
if len (ti[i].split()) + len (ti[i+1].split ()) <= max_words:
mrg = " ".join ([ti[i], ti[i+1]])
ti = [mrg] + ti[2:]
break
if len (ti[-2].split ()) + len (ti[-1].split ()) <= max_words:
mrg = " ".join ([ti[-2], ti[-1]])
ti = ti[:-2] + [mrg]
return '\n'.join (ti)
Примеры:
In : split_title_line ('Primary school completion (% of girls)')
out:
Primary school completion
(% of girls)
In: split_title_line ('Primary school completion in the country as % of girls')
Out:
Primary school completion in the
country as % of girls
Если вы хотите разделить заголовки в matplotlib или около того, вы можете добавить это ax.set_title(split_title_line(r'Normalized occupied Neighbors', max_words=2))
Надеюсь, что все выиграют от этого.